954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

[Linker error] undefined reference to...

In my program that uses separate compilation, the only problem I have left is a linker error. To see if it was just a syntax error somewhere obscure, I made a simple "hello world" program that uses separate compilation. I get the same error. The code is below. If someone knows what is wrong, that would be awesome if they could help.
The header file:

//file name: Hello.h
#ifndef HELLO_H
#define HELLO_H

#include <iostream>
using namespace std;

class Hello
{
      public:
             Hello(int x);
};

#endif //HELLO_H


The implementation file:

//file name: Hello.cpp
#include <iostream>
#include "Hello.h"
using namespace std;

Hello::Hello (int x)
{
            cout << "Hello world";
}


The application file:

//File name: HelloApp.cpp
#include <iostream>
#include "Hello.h"
using namespace std;

int main()
{
    char b;
    cout << "Press a key to test: ";
    cin >> b;
    Hello testing(b);
    return 0;
}


I don't know why, but the error I get is " [Linker error] undefined reference to `Hello::Hello(int)' ". Please help. Thanks.

iamthesgt
Junior Poster
107 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Because you are passing a char to a int variable.

Saith
Junior Poster
118 posts since Dec 2010
Reputation Points: 86
Solved Threads: 24
 

Odd though, I just created this. It gave me no linking error >< Even with a char passed to an int

Saith
Junior Poster
118 posts since Dec 2010
Reputation Points: 86
Solved Threads: 24
 

Im going check on a linux compiler. It may be a compiler issue.

iamthesgt
Junior Poster
107 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

You need to compile both cpp files in the same compilation:

$ g++ HelloApp.cpp Hello.cpp -Wall -o HelloApp


Or, on an IDE (like VS or Code.Blocks), you would need to add all the .cpp files to the list of "source files" for the project (or solution).

mike_2000_17
Posting Virtuoso
Moderator
2,136 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 

Thank you so much! That is exactly what the problem was! I just have to build my makefile. I built it and ran it, but I'm not sure exactly what it is supposed to do.

iamthesgt
Junior Poster
107 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

It says "make: Nothing to be done for 'SSmakefile'." How is the program run? And is this the right result?
Here is my Makefile:

//File Name: SSmakefile

##TARGET: Dependencies
##    Instructions


SumnerApp1.exe: SumnerApp1.o SSassg1.o
    g++ SumnerApp1.o SSassg1.o -o SumnerApp1.exe
SumnerApp1.o: SumnerApp1.cpp SSassg1.h
    g++ -c SomeApp1.cpp
SSassg1.o: SSassg1.cpp SSassg1.h
    g++ -c SSassg1.cpp
clean: 
    rm -f SumnerApp1.o SSassg1.o SumnerApp1.exe


Is it just not compiling the member programs? (The programs are SumnerApp1.cpp, SSassg1.cpp, and SSassg1.h)

iamthesgt
Junior Poster
107 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: