【问题标题】:error C2143: syntax error : missing ';' before '*' (haven't found solution)错误 C2143:语法错误:缺少“;”在 '*' 之前(尚未找到解决方案)
【发布时间】:2016-01-07 10:58:39
【问题描述】:

我的代码中有一个编译错误:

#include <iostream>
#include <vector>
#include "command.h"

extern std::istream *instream;
extern std::vector<command> commands;

这里是 command.h 头文件:

#define CMD_RETURN_TYPE_NONE 0
#define CMD_RETURN_TYPE_STRING 1
#define CMD_RETURN_TYPE_CHAR 2
#define CMD_RETURN_TYPE_INT 3
class command {
    public:
        virtual int getReturnType(void);
        virtual char getOpName(void);
        virtual void* call(void);
}

抛出下一个编译错误

1>------ Build started: Project: MyFirstCPPApp, Configuration: Debug Win32 ------
1>Compiling...
1>MyFirstCPPApp.cpp
1>e:\anton\msvc++ projects\myfirstcppapp\myfirstcppapp\MyFirstCPPApp.h(5) : error C2143: syntax error : missing ';' before '*'
1>e:\anton\msvc++ projects\myfirstcppapp\myfirstcppapp\MyFirstCPPApp.h(5) : error C2377: 'std::istream' : redefinition; typedef cannot be overloaded with any other symbol
1>        E:\Anton\MSVS2008Express\VC\include\iosfwd(707) : see declaration of 'std::istream'
1>e:\anton\msvc++ projects\myfirstcppapp\myfirstcppapp\MyFirstCPPApp.h(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\MyFirstCPPApp.cpp(4) : error C2039: 'get' : is not a member of 'System::Int32'
1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32'
1>.\MyFirstCPPApp.cpp(7) : error C3861: 'callCmd': identifier not found
1>.\MyFirstCPPApp.cpp(11) : error C2440: '=' : cannot convert from 'std::istream *' to 'int *'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Build log was saved at "file://e:\Anton\MSVC++ Projects\MyFirstCPPApp\MyFirstCPPApp\Debug\BuildLog.htm"
1>MyFirstCPPApp - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我还没有找到解决办法,如何解决?

【问题讨论】:

  • 什么是command?分别为command.h
  • @πάντα ῥεῖ 是的,command.h,它是抽象类
  • 您不能将抽象类存储在向量中。而是使用指针。
  • 您在类定义后缺少;
  • @username.ak 这就像说“我不会用英语说 'the' 或 'a',因为没有俄语文章。” C++ 不是 Java,将其视为一种独特的语言。该语言可以谈论类似的概念,但它使用自己的语法、单词和术语来做到这一点。

标签: c++ visual-c++ compilation compiler-errors


【解决方案1】:

缺少;之后:

类命令{ ... };

右括号和分号(表示行尾)之间的任何内容都将被解析为属于此类的对象的可选列表。

因此,有必要在类定义之后添加分号,以便编译器知道该类定义之后的任何内容都不是对象列表的一部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多