提问者:小点点

编译C++代码时未定义引用构造函数


当我尝试用g++main.cpp编译三个C++文件时,收到以下错误。如果我把它们合并在一个文件里,它就能工作了。

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

Time.cpp

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

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

时间。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;
}

共1个答案

匿名用户

您需要编译所有的CPP文件,因为每个文件都是单独的编译单元

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

有关更多信息,请阅读

  • 在C++中如何定义编译单元?
  • 什么是C++中的“翻译单元”

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(编译|c++|代码|未定义|引用|构造|函数)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?