Jump to content
  • Advertisement
Sign in to follow this  
fpsgamer

[C++] Class instance being used without being defined...

This topic is 4121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I imagine this is a very silly question, but please consider the following code:
class Base
{
public:
   void setValue(int v) { value = v; }
protected:
   int value;
};

void doStuff(Base b){
    ...
}

main()
{
	Base b;
	//b.setValue(1);
	 
        doStuff(b);
}





I keep get the error that b is being used without being defined. As I understand it when I declare 'b' it should be constructed using its default empty constructor (which should be inserted automatically). However I notice that I no longer get the error if I: 1) Explicitly insert an empty default constructor 2) Call a member function of Base before I call doStuff(). [edit] 3) Declare 'b' outside of 'main()' [/edit] Whats going on? I hope that someone can help clear this up for me :) [Edited by - fpsgamer on August 5, 2007 11:33:29 PM]

Share this post


Link to post
Share on other sites
Advertisement
Can you post the exact error message, and perhaps add a comment indicating which line generates the error?

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Can you post the exact error message, and perhaps add a comment indicating which line generates the error?


The line that generates the error is:

'doStuff(b);'

The (runtime) error generated is: The variable 'b' is being used without being defined

This is under Visual Studio 2003.

I don't understand how control can flow over the declaration of 'b' and not have it constructed.

[Edited by - fpsgamer on August 5, 2007 11:18:26 PM]

Share this post


Link to post
Share on other sites
void doStuff(Base b){
...
}


Try changing that to a different variable name. You're declaring Base b twice if my knowledge serves me correctly. Once in the function definition and once in your main. Let me know if this helps.

EDIT: Not sure if that's helpful at all now that I think of the scope the variable is in. :slap forehead:

Share this post


Link to post
Share on other sites
Quote:
Original post by Kchaa
void doStuff(Base b){
...
}


Try changing that to a different variable name. You're declaring Base b twice if my knowledge serves me correctly. Once in the function definition and once in your main. Let me know if this helps.
The two b's have different scope, so that shouldn't be a problem. (I have to admit I'm not sure what the problem is though...)

Edit: It might be worth posting the entire example program, including the body of doStuff() and any preprocessor directives.

Share this post


Link to post
Share on other sites
Quote:
Edit: It might be worth posting the entire example program, including the body of doStuff() and any preprocessor directives.


Yeah, that would definitely help. I don't think your problem lies in the code you supplied.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kchaa
Quote:
Edit: It might be worth posting the entire example program, including the body of doStuff() and any preprocessor directives.


Yeah, that would definitely help. I don't think your problem lies in the code you supplied.


I have supplied the whole application. The doStuff() function is literally empty. This is just a small test App I am using to verify my understanding of a C++ concept.

Share this post


Link to post
Share on other sites
Quote:
Original post by fpsgamer
I have supplied the whole application. The doStuff() function is literally empty. This is just a small test App I am using to verify my understanding of a C++ concept.
It should be int main(), but other than that I'm at a loss. I don't have VS2003 set up at the moment, but it compiles fine for me using GCC 4.x.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
Original post by fpsgamer
I have supplied the whole application. The doStuff() function is literally empty. This is just a small test App I am using to verify my understanding of a C++ concept.
It should be int main(), but other than that I'm at a loss. I don't have VS2003 set up at the moment, but it compiles fine for me using GCC 4.x.


wtf? Now I'm stumped. I would really appreciate it if someone else with Visual Studio 2003 gave it a shot so I'm sure I'm not crazy.

[edit]

I noticed you said it 'compiles' fine. Does it run fine too? :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!