对clockType :: clockType(int,int,int)的未定义引用

时间:2014-02-03 00:40:44

标签: c++

我正在尝试创建一个将运行此主程序的类,但我收到了错误:

任何人都可以解释所述错误的原因/解决方案吗?

这是我的主要内容:

int main()
{
    clockType c1(15, 45, 30), c2(3, 20);  // hour, min, sec
    cout << "c1 is " << c1;   // add whatever to beautify it
    cout << "c2 is " << c2;
    cout << "c1+c2 is " <<  c1+c2;
    c2 = c1+c1;
    cout << "c1+c1 is " << c2;
}

这是我的头文件:

#ifndef CLOCKTYPE_H
#define CLOCKTYPE_H
#include <iostream>
#include <ostream>


class clockType
{
friend std::ostream& operator<<(std::ostream& os, const clockType& out);
friend clockType operator+(const clockType& one, const clockType& two);
public:
    clockType();
    clockType(int hours, int minutes, int seconds);
    clockType(int hours, int minutes);
    void setTime(int hours, int minutes, int seconds);
    void getTime(int& hours, int& minutes, int& seconds);
    void printTime();
    void incrementhr();
    void incrementmin();
    void incrementsec();
private:
    int hrs;
    int mins;
    int secs;
};

#endif // CLOCKTYPE_H

这是我的cpp文件:

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

clockType::clockType()
{
    hrs = 0;
    mins = 0;
    secs = 0;
}

clockType::clockType(int hours, int minutes, int seconds)
{
    setTime(hours, minutes, seconds);
}

clockType(int hours, int minutes)
{
    hrs = hours;
    mins = minutes;
    secs = 0;
}

void clockType::setTime(int hours, int minutes, int seconds)
{
    if (0 <= hours && hours < 24)
        hrs = hours;
    else
        hrs = 0;
    if (0 <= minutes && minutes < 60)
        mins = minutes;
    else
        mins = 0;
    if(0 <= seconds && seconds < 60)
        secs = seconds;
    else
        secs = 0;
}

ostream& operator<<(ostream& os, const clockType& out)
{
    os << "Hour is " << out.hrs << "Minute is " << out.mins << "Seconds is " << out.secs;
    return os;
}

clockType operator+(const clockType& one, const clockType& two)
{
    clockType three;
    three.hrs = one.hrs + two.hrs;
    three.mins = one.mins + two.mins;
    three.secs = one.secs + two.secs;
    return three;
}

1 个答案:

答案 0 :(得分:1)

您的.ccp文件包含许多明显的编译错误。例如这个定义

clockType(int hours, int minutes)

已损坏且无法编译。它显然应该是

clockType::clockType(int hours, int minutes)

您没有获得/报告.cpp文件的任何编译错误这一事实意味着您只是忘记将.cpp文件编译为程序的一部分。

这就是编译器无法找到定义的原因。您必须将.cpp文件添加到项目/ makefile /命令行(无论您使用什么)。然后你将不得不修复该文件中存在的编译错误。