Dave
Dave

Reputation: 3

Undefined reference constructor while compiling C++ code

I receive the following error when I try to compile three c++ files with g++ main.cpp. If I combine them in one file, it works.

main.cpp:(.text+0x10): undefined reference to `Time::Time()'

Time.cpp

#include <iostream>
#include "Time.h"
using namespace std;

Time::Time()
{
    a=5;
}

Time.h

#ifndef TIME_H
#define TIME_H

class Time {

public:
Time();
private:
int a;
};
#endif

main.cpp

#include <iostream>
#include "Time.h" 
using namespace std;


int main()
{
    Time t;
}

Upvotes: 0

Views: 959

Answers (1)

phuclv
phuclv

Reputation: 41764

You need to compile all the CPP files because each one is a separate compilation unit

g++ main.cpp Time.cpp -o main

For more information about that read

Upvotes: 3

Related Questions