Pea*_*Gen 1 c++ protected abstract visual-studio-2010
请看下面的代码
游戏对象.h
#pragma once
class GameObject
{
protected:
int id;
public:
int instances;
GameObject(void);
~GameObject(void);
virtual void display();
};
Run Code Online (Sandbox Code Playgroud)
游戏对象.cpp
#include "GameObject.h"
#include <iostream>
using namespace std;
static int value=0;
GameObject::GameObject(void)
{
value++;
id = value;
}
GameObject::~GameObject(void)
{
}
void GameObject::display()
{
cout << "Game Object: " << id << endl;
}
Run Code Online (Sandbox Code Playgroud)
回合.h
#pragma once
#include "GameObject.h"
class Round :
public GameObject
{
public:
Round(void);
~Round(void);
};
Run Code Online (Sandbox Code Playgroud)
圆形.cpp
#include "Round.h"
#include "GameObject.h"
#include <iostream>
using namespace std;
Round::Round(void)
{
}
Round::~Round(void)
{
}
void display()
{
cout << "Round Id: " << id;
}
Run Code Online (Sandbox Code Playgroud)
我 'id' : undeclared identifier在Round课堂上出错。为什么是这样?请帮忙!
在这个函数中:
void display()
{
cout << "Round Id: " << id;
}
Run Code Online (Sandbox Code Playgroud)
您正在尝试访问id在非成员函数中命名的变量。编译器无法解析该名称,因为id它不是任何全局变量或局部变量的名称,因此您会收到一个错误,抱怨未声明标识符。
如果您打算创建display()的成员函数Round(),则应该将其声明为:
class Round : public GameObject
{
public:
Round(void);
~Round(void);
void display(); // <==
};
Run Code Online (Sandbox Code Playgroud)
并以这种方式定义:
void Round::display()
// ^^^^^^^
{
...
}
Run Code Online (Sandbox Code Playgroud)
这样, functionRound::display()将覆盖 virtual function GameObject::display()。
| 归档时间: |
|
| 查看次数: |
3020 次 |
| 最近记录: |