Sign in to follow this  
fpsgamer

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

Recommended Posts

fpsgamer    856
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
jyk    2094
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
fpsgamer    856
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
Kchaa    138
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
jyk    2094
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
Kchaa    138
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
fpsgamer    856
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
jyk    2094
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
fpsgamer    856
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
jyk    2094
Quote:
Original post by Kchaa
In VS2005 I'm getting the same error at run-time.
It's a compile-time error - what do you mean by 'I'm getting the same error at run time'?

Share this post


Link to post
Share on other sites
Kchaa    138
My theory on it is that the declaration in the function definition isn't supplying the default constructor, hence the manually entering the blank constructor fixing the problem.

Share this post


Link to post
Share on other sites
fpsgamer    856
Quote:
Original post by jyk
Quote:
Original post by Kchaa
In VS2005 I'm getting the same error at run-time.
It's a compile-time error - what do you mean by 'I'm getting the same error at run time'?


I'm actually getting this error at runtime.

[Edited by - fpsgamer on August 6, 2007 12:45:54 AM]

Share this post


Link to post
Share on other sites
Kchaa    138
Quote:
It's a compile-time error - what do you mean by 'I'm getting the same error at run time'?


The program compiles fine but after it begins running (console window open) it will crash out. Much like when a program you're running brings up that annoying "Send Error Report" error on windows XP.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by fpsgamer
Quote:
Original post by jyk
Quote:
Original post by Kchaa
In VS2005 I'm getting the same error at run-time.
It's a compile-time error - what do you mean by 'I'm getting the same error at run time'?


I'm actually also getting this error at runtime.
Ok, but it doesn't make much sense to say that you're getting the same error at run time. That would suggest that when the program is run, a little dialog box is popping up that says 'the variable b is being used without being defined', which seems unlikely at best.

Also, how are you getting a run-time error at all if the program won't compile to begin with? :S

Share this post


Link to post
Share on other sites
fpsgamer    856
Quote:
Original post by jyk
Quote:
Original post by fpsgamer
Quote:
Original post by jyk
Quote:
Original post by Kchaa
In VS2005 I'm getting the same error at run-time.
It's a compile-time error - what do you mean by 'I'm getting the same error at run time'?


I'm actually also getting this error at runtime.
Ok, but it doesn't make much sense to say that you're getting the same error at run time. That would suggest that when the program is run, a little dialog box is popping up that says 'the variable b is being used without being defined', which seems unlikely at best.

Also, how are you getting a run-time error at all if the program won't compile to begin with? :S


Who said it wouldn't compile? It compiles fine.

What I'm getting is a runtime error. The executable has debug symbols. Thats how it is able to generate that error message.

What you described is exactly what is happening despite your disbelief.

Share this post


Link to post
Share on other sites
Kchaa    138
The program compiles fine. The error says:

Run-Time Check Failure #3 - The variable 'b' is being used without being defined.

The program window is behind this error, the program compiles fine with one warning:

uninitialized local variable 'b' used

I don't think the declaration of Base b in the function definition is calling the default constructor for some reason. Perhaps the compiler?

Share this post


Link to post
Share on other sites
On Visual C++ 2005 Express Edition it compiles and runs without errors or crashes.

When I compile, I get the warning, though:
warning C4700 : uninitialized local variable 'b' used

I get this on the line:
doStuff(b);

Share this post


Link to post
Share on other sites
RevSundar    122
I Think in your code you have not initialized the class. though i am not sure why default constructor is not called if you call the default constructor by yourself then the code runs fine.

the original code runs fine on VC6 that is because VS2003 is much more strict in stack failure than VC6. so VS2003 catches the bug but not VS2003 i guess.


class Base
{
public:
void setValue(int v) { value = v; }
protected:
int value;
};

void doStuff(Base b){

}

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


Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by fpsgamer
It compiles fine.
Oh :-|

Well, looking back at your original post, I probably should have picked up on the fact that it was a run-time rather than compile-time error. 'Defined' seems like kind of an odd word choice here - GCC's 'b is used uninitialized in this function' makes more sense to me - but what do I know :/ I guess it's a VS thing (by the way, a good place to start with errors like this is Google - a search for 'variable being used without being defined' turns up a number of threads and such related to the same problem).

Anyway, sorry for the confusion.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this