【发布时间】:2015-02-06 00:39:58
【问题描述】:
作为我创建的类的一部分,我有一个表示常量缓冲区的结构。在接口文件(.h 文件)中有一个 getter 函数,它的返回类型与 struct 的返回类型相同。 .h 文件中的函数原型可以被编译器很好地识别,但是在函数定义的实现文件(.cpp 文件)中,编译器会在函数的返回类型下划线,表示它是未定义的。头文件包含在 .cpp 文件中,所以我不确定为什么无法识别函数的返回类型。代码如下:
对于头文件:
#ifndef _PLAYERCLASS_H_
#define _PLAYERCLASS_H_
//Std library includes
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <vector>
#include <memory>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>
// include the Direct3D Library file
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib")
class PlayerClass
{
private:
// a struct to define the constant buffer
struct CBUFFER
{
D3DXMATRIX Final;
D3DXMATRIX Rotation;
D3DXVECTOR4 LightVector;
D3DXCOLOR LightColor;
D3DXCOLOR AmbientColor;
};
public:
//Functions
PlayerClass(); //Constructor
~PlayerClass(); //Destructor
//Functions to set up player
//Getters
CBUFFER constBuff(); //Return the constant buffer
//Setters
private:
//Some private member variables
//Constant buffer to contain alterations to player etc
CBUFFER pUpdates;
};
#endif
对于 .cpp 文件:
#include "Player.h"
#include <Windows.h>
PlayerClass::PlayerClass()
{
pUpdates.LightVector = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 0.0f);
pUpdates.LightColor = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);
pUpdates.AmbientColor = D3DXCOLOR(0.6f, 0.6f, 0.6f, 1.0f);
}
PlayerClass::~PlayerClass()
{
}
void PlayerClass::initPlayer()
{
}
CBUFFER PlayerClass::constBuff()
{
return pUpdates;
}
未被识别的函数是最后一个函数,特别是红色下划线的 CBUFFER 部分。
【问题讨论】:
-
试试
PlayerClass::CBUFFER PlayerClass::constBuff() -
做到了——请问为什么它不能被识别?顺便谢谢。
-
因为
CBUFFER是PlayerClass的一部分,所以您必须像在src 文件中声明PlayerClass函数一样声明它。
标签: c++ class struct directx-11 return-type