C++ 在类中创建函数的问题
我正在尝试使用play()函数设置一个新类。我不确定我做错了什么,因为我还有其他类,我用类似的方式实现了它们,它们工作得很好。有人能指出我可能在哪里出错吗 .h文件C++ 在类中创建函数的问题,c++,class,C++,Class,我正在尝试使用play()函数设置一个新类。我不确定我做错了什么,因为我还有其他类,我用类似的方式实现了它们,它们工作得很好。有人能指出我可能在哪里出错吗 .h文件 #ifndef GAME_H #define GAME_H #include <string> using namespace std; class Game { public: Game(); void play(); }; #endif 我在编译时遇到以下错误: game.cpp:10: error: ISO
#ifndef GAME_H
#define GAME_H
#include <string>
using namespace std;
class Game {
public:
Game();
void play();
};
#endif
我在编译时遇到以下错误:
game.cpp:10: error: ISO C++ forbids declaration of ‘play’ with no type
game.cpp:10: error: prototype for ‘int Game::play()’ does not match any in class ‘Game’
game.h:16: error: candidate is: void Game::play()
game.cpp:10: error: ‘int Game::play()’ cannot be overloaded
game.h:16: error: with ‘void Game::play()’
第一个错误:
Game::play() {}
应该是
void Game::play() {}
void Game::play() {}
第二个-您有使用名称空间std代码>在标题中。永远不要那样做。不是说错了,而是坏习惯
第三,在标题中有#include
,但不使用字符串,因此它是无用的,可能会影响编译时间
第四,您可以使用new
:)。请谷歌智能指针。这是C++和原始指针的最低使用量。您忘记了返回类型:
您应该将type:void添加为返回类型
void Game::play() {}
void Game::play() {}
Game::play() {}