繁体   English   中英

我试图编译我的代码并得到错误`class TIME不命名类型`中的时间显示

[英]I tried to compile my code and got error `time display in class TIME does not name a type`

我创建了一个名为TIME的新 class 并尝试在 another.cpp 文件中定义一个方法( time_display )。 我添加了 header 文件“time.h”,其中定义了 class TIME 我尝试编译此代码,但time display in class TIME does not name a type

#include <iostream>
#include <chrono>
#include <ctime>
#include <cstdlib>
#include "time.h"

class TIME::time_display() {
    // Program to print digital clock using graphics goes here
    return 0;
}

这是我的 header 文件。 我更改了文件名,但仍然产生相同的错误。

#include <iostream>
#include <chrono>
#include <ctime>
#include <cstdlib>

class TIME {
    int seconds, minutes, hours;
public:
    void time_display();
};

我认为你在这里混淆了几件事。

您在time.h中的 class 声明应该是这样的:

class TIME {       
  public:             
    void time_display(); 
};

您的 class 方法定义应该是这样的。 我们称该文件TIME.cpp

#include "time.h"

void TIME::time_display() {
    // void function doesn't return anything
}

所以现在你可能有三个文件:main.cpp、TIME.cpp 和 time.h。 要编译它,请使用以下示例:

g++ TIME.cpp main.cpp -o time_display

获取名为time_display的可执行文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM