|
-
January 14th, 2010, 06:32 PM
#1
Help With Classes!?
Hey guys i'd really appreciate some help with this, I'm trying to compile the following incredibly basic program (as you can see) but getting errors from the compiler that make no sense to me! 
I've put all the code below, and then a copy of the error messages I'm getting. (by the way I do understand the concept of a class and I do realise a namespace would be better suited for the following but I was working on something using a class that failed and I figured since this also fails exactly the same way, this would be easier for you all to read through)
The Code
test.h - Defines the class
Code:
#pragma once
class test
{
public:
double sum(double base, double add); // function to add "add" to "base" as double
};
test.cpp - Provides the function
Code:
#include "test.h"
double test::sum(double base, double add)
{
return base + add;
}
main.cpp - Program start point
Code:
#include <iostream>
#include "test.cpp"
using namespace std;
int main()
{
test myTest;
cout << "The Sum Of 5 And 4 Is: " << myTest.sum(5, 4) << "\n";
return 0;
}
The Errors
1>Compiling...
1>main.cpp
1>Linking...
1>main.obj : error LNK2005: "public: double __thiscall test::sum(double,double)" (?sum@test@@QAENNN@Z) already defined in test.obj
1>C:\Users\...\Documents\Visual Studio 2008\Projects\class_test\Debug\class_test.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://c:\Users\...\Documents\Visual Studio 2008\Projects\class_test\class_test\Debug\BuildLog.htm"
1>class_test - 2 error(s), 0 warning(s)
-
January 14th, 2010, 06:37 PM
#2
Re: Help With Classes!?
Sorry, I forgot to mention one thing: My classes compile fine when the functions are bundled with the definition. E.g: without a header file.
But I read from my book that it was the norm to define a class seperate from it's functions unless you wanted them "inline" and I had VS2008 generate these files for me which makes me believe the book is probably right since VS2008 seperated them on its own. I only made the code for the function "sum"@"test" and all of "main.cpp"
-
January 14th, 2010, 06:41 PM
#3
Re: Help With Classes!?
Your main.cpp should include test.h, not test.cpp. Then test.cpp and main.cpp are compiled (separately) into two object files which the linker glues together to an executable. Only at the last stage, the link between the function call in main.cpp and the function definition in test.cpp is done.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
January 14th, 2010, 07:01 PM
#4
Re: Help With Classes!?
Lol, I feel very silly now, but very grateful. Thank you.
I assumed I had to include the ".cpp" file becasue that was the file that had an include for the header file and not the other way round, which is confusing and raises the question: how does the compiler know to include the .cpp file? Given that the header never tells it to?
See I could understand it the other way around because the .cpp file explicitly asks it to include the header file, but the header file never asks for the .cpp file. Or is there some major gap in my logic? And am I missing the way in which the #include command works?
Thanks in advance guys,
Dave
-
January 14th, 2010, 07:40 PM
#5
Re: Help With Classes!?
In very simple terms.
Header files contain class and function definitions but not necessarily the actual implementation. The .h file contains the definition of sum, but not the code that makes it work.
.cpp files contain the implementation of things defined in header files. test.cpp contains the implementation of sum.
You #include test.h so the compiler knows what test looks like when it compiles main.cpp. It doesn't need to know what sum does at that point, only what it lookes like.
The compiler will build an object file .obj for each .cpp file it compiles.
The linker merges the .obj files into one executable program.
-
January 14th, 2010, 09:46 PM
#6
Re: Help With Classes!?
 Originally Posted by dHall81
how does the compiler know to include the .cpp file? Given that the header never tells it to?
Because you tell it to by including both cpp files in the Visual Studio project. That's the purpose of projects (or Makefiles)----to ensure everything gets built together.
Strictly speaking, the compiler doesn't know the two cpp files are related at all. They're built entirely separately. But then the linker takes the two compiled files and combines them into an executable.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|