【问题标题】:How to fix this- error: Invalid use of className::className?如何解决这个错误:className::className 的使用无效?
【发布时间】:2020-02-18 16:50:18
【问题描述】:

得到错误:className::className 的使用无效。

试图创建一个表示一天中某个时间的 timeStamp 类,它将秒、分钟、小时存储在特定变量中。遇到主函数第 13 行错误,错误显示为:timeStamp::timeStamp 使用无效 我做错了什么? 有一个头文件,其中声明了所有方法。

源文件cpp:

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

timeStamp::timeStamp()
{
    Hour = 0;
    Minute = 0;
    Second =0;
}
timeStamp::timeStamp(int h, int m, int s){

Hour = h;
Minute = m;
Second = s;

}
int timeStamp::getHour(){

return Hour;
}

int timeStamp::getMinute(){
return Minute;
}
int timeStamp::getSecond(){
return Second;
}

bool timeStamp::operator!=(timeStamp t){

    if (Second ==t.Second && Minute ==t.Minute && Hour ==t.Hour)
        return true;
    else
        return false;

}

void timeStamp::Print()
{
    std:: cout<<"Hour:"<<Hour<<"\nMinute:"<<Minute<<"\nSecond:"<<Second<<std::endl;
}

主要:

#include <iostream>
#include "timeStamp.cpp"

using namespace std;

int main()
{
    timeStamp obj1, obj2;
    int h,m,s;

        cout<<"Give hour minute and second:";
        cin>>h>>m>>s;
        obj1.timeStamp(h,m,s); //error
        cout<<"Give H M S";
        cin>>h>>m>>s;
        obj2.timeStamp(h,m,s);

        bool isEqual = obj1 ! = obj2;
        cout<<isEqual<<endl;

}

【问题讨论】:

    标签: c++ oop object


    【解决方案1】:

    这不是你构造对象的方式。

    您必须在定义时提供构造函数参数。

    以后不能像函数一样调用构造函数。

    int main()
    {
        int h,m,s;
    
        cout<<"Give hour minute and second:";
        cin>>h>>m>>s;
        timeStamp obj1(h,m,s);
    
        cout<<"Give H M S";
        cin>>h>>m>>s;
        timeStamp obj2(h,m,s);
    
        bool isEqual = obj1 ! = obj2;
        cout<<isEqual<<endl;
    }
    

    我相信您的书中对此进行了更详细的介绍。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 2016-04-23
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多