首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++对象作为参数

C++对象作为参数
EN

Stack Overflow用户
提问于 2015-01-21 19:56:56
回答 2查看 401关注 0票数 0

我试图创建一个程序,在这个程序中,我需要将对象作为参数传递给类中的函数,但它会给出以下错误:

--开始构建:项目: 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

代码语言:javascript
复制
#include "Game.h"
#include "Chapter.h"

int main(void)
{
Game game;
Chapter hi;

game.addChapter(&hi);
game.start();
return 0;
}

Game.cpp

代码语言:javascript
复制
#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

代码语言:javascript
复制
#pragma once
class Game
{
public:
    Game();
    ~Game();

    void addChapter(Chapter *chapter);
    void start();
};

Chapter.cpp

代码语言:javascript
复制
#include "Chapter.h"

Chapter::Chapter()
{

}


Chapter::~Chapter()
{

}


void Chapter::getInput()
{
cout << "Hello";
}

Chapter.h

代码语言:javascript
复制
#pragma once
class Chapter
{
public:
    Chapter();
    ~Chapter();

protected:
    void getInput();
};

为什么我会得到这个错误,我如何解决这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-21 20:08:05

Chapter.h包含在Game.h中。

代码语言:javascript
复制
\game.h(8): error C2061: syntax error : identifier 'Chapter

当编译器在Chapter中读取addChapter的定义时,您需要知道编译器在哪里可以找到它的定义。

票数 3
EN

Stack Overflow用户

发布于 2015-01-21 20:16:36

您在Game.h中缺少了一个包含到第一章的

代码语言:javascript
复制
#include "Chapter.h"

#pragma once
class Game
{
public:
    Game();
    ~Game();

    void addChapter(Chapter *chapter);
    void start();
};

所有的事情都像你期望的那样运作

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28075761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档