class Base
{
public:
void setValue(int v) { value = v; }
protected:
int value;
};
void doStuff(Base b){
...
}
main()
{
Base b;
//b.setValue(1);
doStuff(b);
}
[C++] Class instance being used without being defined...
I imagine this is a very silly question, but please consider the following code:
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]
Can you post the exact error message, and perhaps add a comment indicating which line generates the error?
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]
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:
Quote:Original post by KchaaThe 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...)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: It might be worth posting the entire example program, including the body of doStuff() and any preprocessor directives.
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.
Quote:Original post by KchaaQuote: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.
Quote:Original post by fpsgamerIt 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.
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.
Quote:Original post by jykQuote:Original post by fpsgamerIt 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.
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.
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? :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement