为什么不能成功编译? [英] Why won't it compile successfully?

查看:87
本文介绍了为什么不能成功编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <string>
using namespace std;

class Sales_item
{
private:
    string m_strName;
public:
    Sales_item(string name):m_strName(name){};
    void Display(ostream& os) {os << m_strName}; /*This line is wrong.*/
};







当我尝试编译上面的代码时,它会显示错误C2143:语法错误:缺少';''之前'}' 。我需要解释为什么它不会编译。在此先感谢。




When I try to compile the code above, it says "error C2143: syntax error : missing ';' before '}'". I need an explanation why it won't compile. Thanks in advance.

推荐答案

我认为你的问题渗透了足够的时间,所以我会跳进去做一些小的贡献。你没有在正确的地方使用分号。



你的代码:

Your question has percolated for enough time I think, so I'll jump in and make a small contribution. You don't have your semicolons in the right place.

Your code:
Sales_item(string name):m_strName(name){};
void Display(ostream& os) {os << m_strName};



应该是:


should be:

Sales_item(string name):m_strName(name){}  // (no semicolon)
void Display(ostream& os) {os << m_strName;} // (semicolon before brace not after)



在结束括号之后使用分号在语法上不正确,但在类定义中它们是多余的...


It is not syntactically incorrect to have semicolons after the closing braces, but they are superfluous in a class definition...


C / C ++需要分号作为结束命令指示器。您的 Display()函数包含必须终止的命令。所以必须是:

The semicolon is required with C/C++ as end of command indicator. Your Display() function contains a command that must be terminated. So it must be:
void Display(ostream& os) {os << m_strName;}



此处不需要分号,因为定义了 Display()函数(包含实现)并且未声明。


The semicolon at the end is not required here, because the Display() function is defined (containing the implementation) and not declared.


这篇关于为什么不能成功编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆