Included Files and undeclared Identifiers

This is a discussion on Included Files and undeclared Identifiers within the C++ Programming forums, part of the General Programming Boards category; Ok, this is probably some simple problem to fix, but I cannot figure out what is wrong with it.
What ...

Included Files and undeclared Identifiers

Ok, this is probably some simple problem to fix, but I cannot figure out what is wrong with it.

What is happening is that when I put some functions and globally define any variables in my programs (in separate files from my 'main.cpp') and #include them, the compiler/linker are telling me that I haven't declared them yet, but I know I have because they are right there. It doesn't matter where I put them either it always tells me that they are "undeclared".

I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

For example:

Without code tags:

for(int i=0;i<5;i++)
{
cout << "No code tags are bad";
}

With Code Tags:

Code:

for(int i=0;i<5;i++)
{
cout << "This code is easy to read";
}

This is of course a basic example...more complicated code is even easier to read with code tags than without.

I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.

First off, I'm sorry about that kermi3 I didn't notice the buttons above the textbox, i will use them next time.

bithub: I can't do that for every variable/object in my program though. Plus there is a program's source that I downloaded off the net and it successfully does what I'm trying to do. And I have examined the code many times and it is no different from mine.

It is generally bad practice to include .cpp files (the only exception I can think of is inline/template functions, and even then I think you should use a .inl file or include the definitions in the header)

ok, Thank you all for your input. Yeah, I know including .cpp files in the code is bad practice, but with my code it should have worked, no? I was just trying to get some quick and dirty code done and it was really making me pull my hair out because i couldn't see any reason for it not to work.

But, VS was being a pain again. First off, I thought it seemed like it was trying to compile each file individually. But i figured out that if i go to the properties of each included source file and change to exclude from build and change to YES. then everything works fine. Thanks though!

Yeah, I know including .cpp files in the code is bad practice, but with my code it should have worked, no?

No, it shouldn't have, as I will explain below.

But i figured out that if i go to the properties of each included source file and change to exclude from build and change to YES. then everything works fine.

That's because they're supposed to be compiled individually. The problem is, the code in the included source files doesn't make sense without the other code that's in main.cpp... so main.cpp will successfully compile (hopefully) with all the contents of the included source files glued in at the top, but the others will not. So in other words, what's happening is:

The proper way of solving this problem is, as has been mentioned above:
-Declare the variables as 'extern' at the top of each .cpp
-Include the .h file at the top of your .cpp file.
-Stick the functions' prototypes in a .h file and include that in main.cpp

Or, you could rename the .cpp files to .h or .inl. But excluding the .cpp's from the build is NOT a good solution at all.

That wouldn't be a good solution. The .cpp files would still be compiled, and if they're also being included in main.cpp, then there's no need for them to be compiled; besides, you might even get redefinition errors from your linker, since the very same functions are defined both in main.cpp and in x.cpp.

All I was suggesting is that you put the externs in a header file. As long as the globals are defined only once i don't see how it would be any different then placing the extern statemements manually in each file? Maybe i am not fully understanding the problem.

As long as the globals are defined only once i don't see how it would be any different then placing the extern statemements manually in each file?

I'm not talking about the externs. What I'm referring to is the fact that both the main.cpp with the included functions inlined and the other .cpp files are compiled. Thus, two of the same function are compiled; and if the linker isn't smart enough to exclude the other .cpp files' compiled object files, then you'll either end up with linker errors from function redefinition (although that should probably be a compiler error), or else you'll end up with duplicate code bloating your .exe file.