来自Java / Eclipse世界,只想了解C ++并使用Code :: Blocks。
正在制作一个基础课程,并且无法将其实施到我的主文件中。
将“未定义的引用'Time :: Time(int,int,int)'”和未定义的引用错误添加到我的main.cpp文件中使用的所有方法中。
尝试重建,尝试将文件重新添加到控制台项目中。不确定是什么问题。我对Code :: Blocks相当生疏,所以也许它显而易见,任何帮助都会受到赞赏。
的main.cpp
#include "Time.h"
#include <iostream>
using namespace std;
int main()
{
//Time current();
Time current(12,0,0);
cout << "Current time." << endl;
current.displayTime();
cout << "\nSet time to 22:29:30." << endl;
current.setTime(22,29,30);
current.displayTime();
cout << "\nAdd 45 minutes." << endl;
current.addMinutes(45);
current.displayTime();
cout << "\nAdd 45 seconds." << endl;
current.addSeconds(45);
current.displayTime();
cout << "\nAdd 1 hour." << endl;
current.addHours(1);
current.displayTime();
return 0;
}
time.h中
#ifndef TIME_H
#define TIME_H
class Time
{
public:
Time(int h, int m, int s);
Time(int h, int m);
Time(int h);
Time();
virtual ~Time();
void setTime(int h, int m, int s);
void setTime(int h, int m);
void setTime(int h);
void addHours(int h);
void addMinutes(int m);
void addSeconds(int s);
void displayTime();
int getHours();
void setHours(int h);
int getMinutes();
void setMinutes(int m);
int getSeconds();
void setSeconds(int s);
protected:
private:
int hours;
int minutes;
int seconds;
};
#endif // TIME_H
Time.cpp
#include "Time.h"
#include <iostream>
using namespace std;
Time::Time(int h, int m, int s)
{
hours = h;
minutes = m;
seconds = s;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
seconds = 0;
}
Time::Time(int h)
{
hours = h;
minutes = 0;
seconds = 0;
}
Time::Time()
{
hours = 0;
minutes = 0;
seconds = 0;
}
Time::~Time()
{
//dtor
}
Time::void setTime(int h, int m, int s)
{
hours = h;
minutes = m;
seconds = s;
}
Time::void setTime(int h, int m)
{
hours = h;
minutes = m;
seconds = 0;
}
Time::void setTime(int h)
{
hours = h;
minutes = 0;
seconds = 0;
}
Time::void addHours(int h)
{
hours += h;
if (hours > 23)
{
hours -= 24;
}
}
Time::void addMinutes(int m)
{
minutes += m;
if (minutes > 59)
{
addHours(minutes / 60);
minutes %= 60;
}
}
Time::void addSeconds(int s)
{
seconds += s;
if (seconds > 59)
{
addMinutes(seconds / 60);
seconds %= 60;
}
}
Time::void displayTime()
{
if (hours < 10)
{
cout << "0" << hours;
}
else
{
cout << hours;
}
cout << ":";
if (minutes < 10)
{
cout << "0" << minutes;
}
else
{
cout << minutes;
}
cout << ":";
if (seconds < 10)
{
cout << "0" << seconds;
}
else
{
cout << seconds;
}
cout << endl;
}
Time::void setHours(int h)
{
if (h >= 0 && h <= 23)
{
hours = h;
}
else
{
cout << "Invalid input!\n";
}
}
Time::int getHours()
{
return hours;
}
Time::void setMinutes(int m)
{
if (m >= 0 && m <= 59)
{
minutes = m;
}
else
{
cout << "Invalid input!\n";
}
}
Time::int getMinutes()
{
return minutes;
}
Time::void setSeconds(int s)
{
if (s >= 0 && s <= 59)
{
seconds = s;
}
else
{
cout << "Invald input!\n";
}
}
Time::int getSeconds()
{
return seconds;
}
答案 0 :(得分:2)
类名在方法名之前,而不是在返回类型之前。
所以而不是像。
Time::void setTime(int h)
{
hours = h;
minutes = 0;
seconds = 0;
}
应该是
void Time::setTime(int h)
{
hours = h;
minutes = 0;
seconds = 0;
}
Time.cpp由于错误而无法编译,然后链接器找不到任何符号。