我试图创建一个程序,在这个程序中,我需要将对象作为参数传递给类中的函数,但它会给出以下错误:
--开始构建:项目: C++程序,配置:调试Win32 Main.cpp ...\game.h(8):错误C2061:语法错误:标识符“章节” ...\main.cpp(9):C2660:‘对策::加载项’:函数不带1个参数 Game.cpp ...\game.h(8):错误C2061:语法错误:标识符“章节” ...\game.cpp(20):错误C2511:‘无效游戏::添加章节(第*章)’: “游戏”中找不到的超负荷成员函数 ...\game.h(3):见“游戏宣言”
我的守则:
Main.cpp
#include "Game.h"
#include "Chapter.h"
int main(void)
{
Game game;
Chapter hi;
game.addChapter(&hi);
game.start();
return 0;
}Game.cpp
#include "Game.h"
#include <iostream>
#include <vector>
#include <string>
#include "Chapter.h"
using namespace std;
Game::Game()
{
}
Game::~Game()
{
}
void Game::addChapter(Chapter *chapter)
{
cout << "Added";
}
void Game::start()
{
cout << "Started";
}Game.h
#pragma once
class Game
{
public:
Game();
~Game();
void addChapter(Chapter *chapter);
void start();
};Chapter.cpp
#include "Chapter.h"
Chapter::Chapter()
{
}
Chapter::~Chapter()
{
}
void Chapter::getInput()
{
cout << "Hello";
}Chapter.h
#pragma once
class Chapter
{
public:
Chapter();
~Chapter();
protected:
void getInput();
};为什么我会得到这个错误,我如何解决这个问题?
发布于 2015-01-21 20:08:05
将Chapter.h包含在Game.h中。
\game.h(8): error C2061: syntax error : identifier 'Chapter当编译器在Chapter中读取addChapter的定义时,您需要知道编译器在哪里可以找到它的定义。
发布于 2015-01-21 20:16:36
您在Game.h中缺少了一个包含到第一章的
#include "Chapter.h"
#pragma once
class Game
{
public:
Game();
~Game();
void addChapter(Chapter *chapter);
void start();
};所有的事情都像你期望的那样运作
https://stackoverflow.com/questions/28075761
复制相似问题