我对 C++ 完全陌生,但我尝试运行我正在阅读的有关该主题的书中的代码。对于初学者来说,这是一本非常基础的书,但它包含一些代码,我试图将这些代码写入 C++ 以了解它是如何工作的。我尝试了以下代码,但出现了两个错误:
在函数“int main()”中:
[错误] 应为 ';'在'emp'之前
#include <iostream>
#include <ctime>
class Employee
{
private:
int m_id;
public:
Employee(){}
Employee(int id)
{
m_id=id;
}
int id;
std::string firstname;
std::string lastname;
int birthyear;
void ClockTime()
{
//clocktime code goes here
}
~Employee()
{
m_id=0;
}
};
int main()
{
Employee emp(2);
std::cout<<"The employee time clocked is"
emp.ClockTime()<<std::endl;
return 0;
}
为了让这段代码工作,我需要做很多更改吗?
最佳答案
打印时断了一行。应该是:
std::cout<<"The employee time clocked is " <<
emp.ClockTime()<<std::endl;
请记住,C++ 编译器会忽略空白字符。所以在你的版本中,你有这样的东西:
std::cout<<"The employee time clocked is"emp.ClockTime()<<std::endl;
这应该让您清楚为什么会发生错误。
作为gurka指出,您的代码还有许多其他问题。其中最重要的一点是您不能打印 void
(事实上不能用它做很多事情)。所以 ClockTime
应该返回一些东西。
关于c++ - C++ 初学者。获取代码中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41590837/