我对c ++完全不熟悉,但我试图从我正在阅读的关于这个主题的书中运行代码.对于初学者来说,这是一本非常基础的书,但它包含了我试图写入c ++的som代码,以了解它是如何工作的.我尝试了以下代码,但我收到两个错误:
在函数'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;
}
Run Code Online (Sandbox Code Playgroud)
为了使这段代码有效,我还有很多需要改变的地方吗?
你打印时打破了一条线.它应该是:
std::cout<<"The employee time clocked is " <<
emp.ClockTime()<<std::endl;
Run Code Online (Sandbox Code Playgroud)
请记住,C++编译器会忽略空格字符.所以在你的版本中,你有这样的东西:
std::cout<<"The employee time clocked is"emp.ClockTime()<<std::endl;
Run Code Online (Sandbox Code Playgroud)
哪个应该清楚地说明错误发生的原因.
正如gurka指出的那样,您的代码存在许多其他问题.其中最重要的是你不能打印void(事实上不能用它做多少).所以ClockTime应该回复一些东西
| 归档时间: |
|
| 查看次数: |
82 次 |
| 最近记录: |