Can't figure out separate compilation.

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>
using namespace std;
class Test
{
private:
    int yearofbirth, currentyear, age;
    string name;

public:
    Test();
    void dataCapture();
    void calculate();
    void outPut();
}; 


The Implementation file named "implementation.cpp"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include "header.h"
using namespace std;

Test::Test()
{
    yearofbirth = 0;
    currentyear = 0;
    name = " ";
}
void Test::dataCapture()
{
    cout << "please input year of birth :";
    cin >> yearofbirth;
}
void Test::calculate()
{
    age = currentyear - yearofbirth;
}
void Test::outPut()
{
    cout<< "you are "<< age<<" years old";
}


And finally, the application file named "application.cpp"
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "header.h"
#include<iostream>
using namespace std;

int main()
{
    Test t1;
    Test.dataCapture();
    Test.calculate();
    Test.outPut();
    return 0;
}


Thank you in advance for any advice/assistance.
Your main routine should be:

1
2
3
4
5
6
7
8
int main()
{
    Test t1;
    t1.dataCapture();
    t1.calculate();
    t1.outPut();
    return 0;
}


It runs, but you haven't set currentyear, so I have a negative age.
I can't see anything wrong with that code that would stop it building.
I'm an idiot - others have spotted it easily.

Can you be more specific about what your problem is?
Last edited on
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.
if i replace Test.dataCapture(); with t1.dataCapture(); the i get a new error.
"undefined reference to Test::dataCapture()"
1
2
3
4
    Test t1;
    Test.dataCapture();
    Test.calculate();
    Test.outPut();

You put the name of the type, not the name of the variable.

See lastchance's post.
the error of "undefined reference to (some function)" is the same i was getting in the larger program i was working on.
Please @AdvisedSwine23 - just go back and read mine and @Salem C's posts.


I can assure you it compiles and runs when you put t1. rather than Test. in front of those three function calls in main().

It produces a wrong answer, but that's a different problem.
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "header.h"
#include<iostream>
using namespace 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.

Last edited on
@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.
Last edited on
@lastchance thank you for being patient with me(no sarcasm). Im using codeblocks IDE.
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.
Thanks all i figured it out. The reason is embarrassing.
i didnt have the codes in a project.
This is due to me never having needed to use a project.
Doing "stupid stuff" is part of the learning process. Been there, done that. Still do from time to time. :)

I bet you'll never forget to add your files to a project again. :D
Topic archived. No new replies allowed.