c++中遇到:undefined reference to
写了一个clock类:clock。h:#ifndefCLOCK_H_INCLUDED#defineCLOCK_H_INCLUDEDclassClock{private:i...
写了一个clock类:
clock。h:
#ifndef CLOCK_H_INCLUDED
#define CLOCK_H_INCLUDED
class Clock
{
private:
int id;
int Hour, Minute, Second;
private:
bool checkTime(int h, int m, int s);
public:
Clock(int id);
Clock(Clock &c);
Clock(int id, int h, int m, int s);
void setTime(int h,int m, int s);
void showTime();
public:
~Clock(void);
};
#endif // CLOCK_H_INCLUDED
========================
clock.cpp
#include"clock.h"
#include"iostream"
using namespace std;
Clock::Clock(int number)
{//构造函数
id = number;
}
Clock::Clock(int number, int h,int m, int s)
{
id = number;//this 时钟的id
setTime(h, m, s);
}
Clock::Clock( Clock &c )
{
this->id = c.id;
setTime(c.Hour, c.Minute, c.Second);
}
void Clock::setTime(int h,int m, int s)
{
if (checkTime(h, m, s)) {//check 检查时间的合法性
Hour = h;
Minute = m;
Second = s;
}
}
void Clock::showTime()
{
cout<<"clock"<<id<<"->"<<Hour<<":"<<Minute<<":"<<Second<<"\n";
}
Clock::~Clock(void)
{
cout<<"clock"<<id<<" closed\n";
}
bool Clock::checkTime( int h, int m, int s )
{
return true;
}
=============================
main.cpp
#include <iostream>
#include "clock.h"
using namespace std;
int main()
{
//cout << "Hello world!" << endl;
Clock clock1(1);
clock1.showTime();
clock1.setTime(8,30,30);
clock1.showTime();
return 0;
}
在我变异的时候遇到:
obj\Debug\main.o||In function `main':|
S:\c++\practice\MyClock\main.cpp|9|undefined reference to `Clock::Clock(int)'|
S:\c++\practice\MyClock\main.cpp|10|undefined reference to `Clock::showTime()'|
S:\c++\practice\MyClock\main.cpp|11|undefined reference to `Clock::setTime(int, int, int)'|
S:\c++\practice\MyClock\main.cpp|12|undefined reference to `Clock::showTime()'|
S:\c++\practice\MyClock\main.cpp|13|undefined reference to `Clock::~Clock()'|
S:\c++\practice\MyClock\main.cpp|13|undefined reference to `Clock::~Clock()'|
||=== Build finished: 6 errors, 0 warnings ===| 展开
clock。h:
#ifndef CLOCK_H_INCLUDED
#define CLOCK_H_INCLUDED
class Clock
{
private:
int id;
int Hour, Minute, Second;
private:
bool checkTime(int h, int m, int s);
public:
Clock(int id);
Clock(Clock &c);
Clock(int id, int h, int m, int s);
void setTime(int h,int m, int s);
void showTime();
public:
~Clock(void);
};
#endif // CLOCK_H_INCLUDED
========================
clock.cpp
#include"clock.h"
#include"iostream"
using namespace std;
Clock::Clock(int number)
{//构造函数
id = number;
}
Clock::Clock(int number, int h,int m, int s)
{
id = number;//this 时钟的id
setTime(h, m, s);
}
Clock::Clock( Clock &c )
{
this->id = c.id;
setTime(c.Hour, c.Minute, c.Second);
}
void Clock::setTime(int h,int m, int s)
{
if (checkTime(h, m, s)) {//check 检查时间的合法性
Hour = h;
Minute = m;
Second = s;
}
}
void Clock::showTime()
{
cout<<"clock"<<id<<"->"<<Hour<<":"<<Minute<<":"<<Second<<"\n";
}
Clock::~Clock(void)
{
cout<<"clock"<<id<<" closed\n";
}
bool Clock::checkTime( int h, int m, int s )
{
return true;
}
=============================
main.cpp
#include <iostream>
#include "clock.h"
using namespace std;
int main()
{
//cout << "Hello world!" << endl;
Clock clock1(1);
clock1.showTime();
clock1.setTime(8,30,30);
clock1.showTime();
return 0;
}
在我变异的时候遇到:
obj\Debug\main.o||In function `main':|
S:\c++\practice\MyClock\main.cpp|9|undefined reference to `Clock::Clock(int)'|
S:\c++\practice\MyClock\main.cpp|10|undefined reference to `Clock::showTime()'|
S:\c++\practice\MyClock\main.cpp|11|undefined reference to `Clock::setTime(int, int, int)'|
S:\c++\practice\MyClock\main.cpp|12|undefined reference to `Clock::showTime()'|
S:\c++\practice\MyClock\main.cpp|13|undefined reference to `Clock::~Clock()'|
S:\c++\practice\MyClock\main.cpp|13|undefined reference to `Clock::~Clock()'|
||=== Build finished: 6 errors, 0 warnings ===| 展开
2个回答
展开全部
我完全使用你的代码调试了一遍,完全没有问题,可以运行,你是不是工程建得有什么问题。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不知道你用什么编译,我看不出问题,而且也用VC 6试了一下,没有任何错误提示...
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询