I have quite a large program that require separate compilation to function. However I simply can't get the program to work. Having scoured the internet and my textbook for possible issues with my code I decided to make a much smaller and simpler program to lock down the core syntax and ideas of how to compile for separate files. But even with the simple code I have yet to completely compile and run the code. I must surely be missing some crucial part of the syntax or something to do with my IDE(unlikely).
The code bellow is the simple code.
The header/interface named "header.h"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>
usingnamespace std;
class Test
{
private:
int yearofbirth, currentyear, age;
string name;
public:
Test();
void dataCapture();
void calculate();
void outPut();
};
The Implementation file named "implementation.cpp"
But even with the simple code I have yet to completely compile and run the code. I must surely be missing some crucial part of the syntax or something to do with my IDE(unlikely).
What kind of error messages is your compiler/linker producing when you try to compile the code?
Sorry about the negative age(rush job)
the error i get is
"error: expected unqualified-id before '.' token "
i receive this error on line 8,9,10 basically all my function calls.
#include "header.h"
#include<iostream>
usingnamespace std;
int main()
{
Test t1;
t1.dataCapture();
t1.calculate();
t1.outPut();
return 0;
}
This is the application.cpp code after i followed the advice of @lastchance and @salem
Which i did a few posts back but apparently i have been diagnosed with blindness.
The error
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
undefined reference to `Test::Test()'|
undefined reference to `Test::dataCapture()'|
undefined reference to `Test::calculate()'|
undefined reference to `Test::outPut()'|
||error: ld returned 1 exit status|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
With some omissions of file location as to not wanting to dox myself.
i did and literally copied your code and replaced my code with it.And i ran it and the same error from my latest post came up.
That makes no sense. That's a linker error. You can't possibly have run it, because there was nothing to run - the linker wasn't able to create an executable in the first place.
Are you sure that both of your CPP files are being compiled?
Are you sure that the object files the compiler is producing, are all being linked by your linker?
If you're using an IDE, and have properly added both CPP files to the project, this should be happening automatically, but it's worth checking.
Are you sure that you are actually linking implementation.cpp as well as main.cpp?
I work from the command line, so my compile-and-link command would be g++ main.cpp implementation.cpp
(The order of those files doesn't matter).
Make sure that whatever IDE you are using is compiling and linking BOTH .cpp files.
If you state what IDE you are using and how you have told it to compile and link both those files then maybe someone who uses the same IDE can help you.
@MikeyBoy thank you for a reply not implying that I can't take advice the first time lol. My mistake when i say run i mean attempt to run(mashing f9). How would i check that the files are linked. All 3 files are in the same file location and all of them are open in the IDE.
Perhaps I'm/its not "adding" the Cpp files correctly. How can i check this.
I don't use Codeblocks, so I would be a poor guide, but if you "Build" your "Project" what is the output and does it tell you what compiling and linking it is doing?
I'm not sitting at my programming development computer at this time, so any advice I give might be "off" on some details.
Code::Blocks has a window on the left side of the IDE that displays all the files it believes are to be part of the current project. The tree should show 3 files. One header and two cpp files. If you are missing any files you need to add them to the project.
You really should put any custom headers you use/create AFTER all your standard C++ headers you use. The compiler/linker may be getting confused by the current order in your implementation source file.