c++ - 链接错误 : undefined reference to

标签 c++ g++

当我尝试将它们链接在一起时,出现了这个错误。怎么了?我需要放 #include <iostream> 吗?和 using namespace std;在头文件和两个cpp文件中?有没有一种方法我只需要包括 <iostream>using namespace std;一次?提前致谢。

错误:

/tmp/cczgScpr.o: In function `main':
time_overloaded_operators.cpp:(.text+0x45): undefined reference to `Time::Time(unsigned int const&, unsigned int const&, unsigned int const&, unsigned int const&)'
time_overloaded_operators.cpp:(.text+0x63): undefined reference to `operator*(Time const&, int const&)'
time_overloaded_operators.cpp:(.text+0x74): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Time const&)'
time_overloaded_operators.cpp:(.text+0x92): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Time const&)'
time_overloaded_operators.cpp:(.text+0xb0): undefined reference to `Time::~Time()'
time_overloaded_operators.cpp:(.text+0xc3): undefined reference to `Time::~Time()'
time_overloaded_operators.cpp:(.text+0xdc): undefined reference to `Time::~Time()'
time_overloaded_operators.cpp:(.text+0xf6): undefined reference to `Time::~Time()'
collect2: ld returned 1 exit status

主要cpp:

// practice on overloading operators on Time variables
#include <iostream>
#include "Time.h"
using namespace std;
//main function
int main() {
  Time time1(0,1,0,0);
  Time time3 = time1 * 2;
  cout << time1 << endl;
  cout << time3 << endl;
  return 0;
}

头文件:

#ifndef TIME_H
#define TIME_H
#include <iostream>
using namespace std;
class Time {
 public:
  //constructor
  Time(const unsigned int& day, const unsigned int& hour,
       const unsigned int& minute, const unsigned int& second);
  //copy constructor
  Time(const Time& time);
  //assignment operator
  Time& operator=(const Time& time); 
  //destructor
  ~Time(); 
  //member functions
  //overloaded operators
  Time& operator+=(const Time& time); 
  Time operator+(const Time& time);
  friend Time operator*(const Time& time, const int& integer_number);
  friend ostream& operator<<(ostream& output, const Time& time);
  friend istream& operator>>(istream& input, Time& time);
  friend bool operator==(const Time& first_object, const Time& second_object);
  friend bool operator!=(const Time& first_object, const Time& second_object);

 private:
  unsigned int day_;
  unsigned int hour_;
  unsigned int minute_;
  unsigned int second_;
  void ConvertSecondsToTime();
  unsigned int TotalTimeInSeconds() const;
};
#endif

执行文件:

#include "Time.h"
#include <iostream>
// class constructors
Time::Time(const unsigned int& day = 0, const unsigned int& hour = 0,
       const unsigned int& minute = 0, const unsigned int& second = 0)
    : day_(day),
      hour_(hour),
      minute_(minute),
      second_(second) {
}

Time::Time(const Time& time)
    : day_(time.day_),
      hour_(time.hour_),
      minute_(time.minute_),
      second_(time.second_) {
}
Time& Time::operator=(const Time& time) {
    day_ = time.day_;
    hour_ = time.hour_;
    minute_ = time.minute_;
    second_ = time.second_;
    return *this;
}
Time::~Time() {}
  //overloaded operators
unsigned int Time::TotalTimeInSeconds() const {
  return (day_ * 24 * 60 * 60 
          + hour_ * 60 * 60 
          + minute_ * 60 
          + second_);
}
void Time::ConvertSecondsToTime() {
  while (second_ >= 60) {
    second_ -= 60;
    minute_ += 1;
  }
  while (minute_ >= 60) {
    minute_ -= 60;
    hour_ += 1;
  } 
  while (hour_ >= 24) {
    hour_ -= 24;
    day_ += 1;
  }
}
T    ime& Time::operator+=(const Time& time) {
  Time temp;
  temp.second_ = TotalTimeInSeconds() + time.TotalTimeInSeconds();
  *this = temp;
  ConvertSecondsToTime();
  return *this;
}
Time Time::operator+(const Time& time) {
  Time temp(*this);
  temp += time;
  return temp;
}
Time operator*(const Time& time, const int& integer_number) {
  Time temp;
  temp.second_ = time.TotalTimeInSeconds() * integer_number;
  temp.ConvertSecondsToTime();
  return temp;
}
ostream& operator<<(ostream& output, const Time& time) {
  output << "days: " << time.day_ 
         << " hours: " << time.hour_ 
         << " minutes: " << time.minute_
         << " seconds: " << time.second_ << endl;
  return output;
}
istream& operator>>(istream& input, Time& time) {
  input >> time.day_ >> time.hour_ >> time.minute_ >> time.second_;
  if (!input) {
    time = Time();
  }
  return input;
}
bool operator==(const Time& first_object, const Time& second_object) {
  return first_object.TotalTimeInSeconds()
         == second_object.TotalTimeInSeconds();
}
bool operator!=(const Time& first_object, const Time& second_object) {
  return !(first_object == second_object);
}

最佳答案

g++ main.cpp time.cpp 

应该修复您的链接错误,以便 main 可以链接到时间函数。如果 Time 成员函数是在共享库中实现的,则必须将其传递给 linker。 .通常像 -llibraryname

关于c++ - 链接错误 : undefined reference to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8386670/

相关文章:

c++ - 从字符串中删除 BBcode

c++ - gcc 编译 C++ 代码 : undefined reference to `operator new[](unsigned long long)'

c++ - 更紧密地打包位域

linux - 我无法安装 g++

c++ - 在 C++ 中声明大字符数组

c++ - 强制立即处理 Qt 自定义信号?

带有指针的 C++ 类模板特化

c++ - 使用模板构造函数创建模板类对象

c++ - 结构体(位于堆上)的 getter 和 setter 是什么样子的?

c++ - 链接到动态文件和作为输入对象有什么区别?