C++中未定义的类声明,我已经包含了。

huangapple go评论64阅读模式
英文:

C++ undefined class declarations which I have already included

问题

以下是您要翻译的内容:

我的问题涉及包含头文件。我不想在其他头文件中包含头文件,所以当我需要它时,我总是在头文件的开头写声明,然后在cpp文件中包含,正如您所看到的,但这一次不起作用。

BaseModel.h

#pragma once
#include <vector>
#include <unordered_map>
#include <string>

class Texture
{
public:
	void addTexture2D(const char* keyname, int filter, int wrap, int internalformat, int format, int width, height, unsigned char* texelData);
	void addTextureCubeMap(const char* keyname, int filter, int wrap, int internalformat, int format, int width, int height, unsigned char* texelData);
	void bindTexture(const char* keyname, unsigned int additionalUnits = 0);

	inline static Texture* const getInstance() { return m_instance; }
private:
	static Texture* const m_instance;
	std::unordered_map<const char*, unsigned int> m_textureIDs;

	Texture();
	~Texture();

	friend void terminateEngine();
};

class Mesh
{
public:
	Mesh();
	Mesh(const Mesh& copy) = delete;
	Mesh(Mesh&& move) noexcept;
	~Mesh();

	void addVertexBuffer(size_t sizeInByte, const void* data, const unsigned int* perAttribSize, const unsigned int attribCount, unsigned int divisorValue, bool isDynamic = false);
	void addIndexBuffer(size_t sizeInByte, const void* data);

	void Bind() const;
	inline const unsigned int& getVertexArrayId() const { return m_vertexArrayID; }

private:
	unsigned int m_attribIndex;
	unsigned int m_vertexArrayID;
	std::vector<unsigned int> m_buffers;
};

class Program
{
public:
	struct Shaders
	{
		unsigned int vertexShader;
		unsigned int tessControlShader;
		unsigned int tessEvaluationShader;
		unsigned int geometryShader;
		unsigned int fragmentShader;
	};

	Program();
	Program(const Program& copy) = delete;
	Program(Program&& move) noexcept;
	~Program();

	void addShaders(const Shaders& shaders);
	void addComputeShader(const unsigned int computeShader);
	void Bind() const;

	inline const unsigned int& getProgramID() const { return m_programID; }
private:
	unsigned int m_programID;
};

Model.h

#pragma once
#include "Interfaces.h"
#include "glm/glm.hpp"

class Mesh;
class Program;

class Surface
{
public:
	Surface(const unsigned int seed, float chunkSize, float instanceCountperSide);
	Surface(const Surface& copy) = delete;
	Surface(Surface&& move) noexcept = delete;
	~Surface();

	void Render(const glm::vec3& cameraPos);

private:
	Mesh m_mesh;
	Program m_program;

	unsigned int m_indexCount;

	float a;
	float globaltexCoords[2];

	int m_cameraLoc;
	int m_globaltexCoordloc;
};

在Model.cpp中

#include "Model.h"
#include "BaseModel.h"
#include "glad/glad.h"
#include "GLFW/glfw3.h" // Silincek
#include "stb_image.h"
#include "FileIO.h"

问题说Surface::m_mesh和Surface::m_program是未定义的类'Mesh'和'Program',但实际上我已经添加了它们,正如在Model.cpp中所示。为什么会遇到这个问题?

我已经检查了Model.cpp的预处理器文件,没有问题。

英文:

My problem is about including header files. I don't want to include headers in other header file so when I need it, I always write declarations the beginning of the header and include in the cpp files as you can see, but this time it does not work.

BaseModel.h

#pragma once
#include &lt;vector&gt;
#include &lt;unordered_map&gt;
#include &lt;string&gt;
class Texture
{
public:
void addTexture2D(const char* keyname, int filter, int wrap, int internalformat, int format, int width, int height, unsigned char* texelData);
void addTextureCubeMap(const char* keyname, int filter, int wrap, int internalformat, int format, int width, int height, unsigned char* texelData);
void bindTexture(const char* keyname, unsigned int additionalUnits = 0);
inline static Texture* const getInstance() { return m_instance; }
private:
static Texture* const m_instance;
std::unordered_map&lt;const char*, unsigned int&gt; m_textureIDs;
Texture();
~Texture();
friend void terminateEngine();
};
class Mesh
{
public:
Mesh();
Mesh(const Mesh&amp; copy) = delete;
Mesh(Mesh&amp;&amp; move) noexcept;
~Mesh();
void addVertexBuffer(size_t sizeInByte, const void* data, const unsigned int* perAttribSize, const unsigned int attribCount, unsigned int divisorValue, bool isDynamic = false);
void addIndexBuffer(size_t sizeInByte, const void* data);
void Bind() const;
inline const unsigned int&amp; getVertexArrayId() const { return m_vertexArrayID; }
private:
unsigned int m_attribIndex;
unsigned int m_vertexArrayID;
std::vector&lt;unsigned int&gt; m_buffers;
};
class Program
{
public:
struct Shaders
{
unsigned int vertexShader;
unsigned int tessControlShader;
unsigned int tessEvaluationShader;
unsigned int geometryShader;
unsigned int fragmentShader;
};
Program();
Program(const Program&amp; copy) = delete;
Program(Program&amp;&amp; move) noexcept;
~Program();
void addShaders(const Shaders&amp; shaders);
void addComputeShader(const unsigned int computeShader);
void Bind() const;
inline const unsigned int&amp; getProgramID() const { return m_programID; }
private:
unsigned int m_programID;
};

Model.h

#pragma once
#include &quot;Interfaces.h&quot;
#include &quot;glm/glm.hpp&quot;
class Mesh;
class Program;
class Surface
{
public:
Surface(const unsigned int seed, float chunkSize, float instanceCountperSide);
Surface(const Surface&amp; copy) = delete;
Surface(Surface&amp;&amp; move) noexcept = delete;
~Surface();
void Render(const glm::vec3&amp; cameraPos);
private:
Mesh m_mesh;
Program m_program;
unsigned int m_indexCount;
float a;
float globaltexCoords[2];
int m_cameraLoc;
int m_globaltexCoordloc;
};

In Model.cpp

#include &quot;Model.h&quot;
#include &quot;BaseModel.h&quot;
#include &quot;glad/glad.h&quot;
#include &quot;GLFW/glfw3.h&quot; // Silincek
#include &quot;stb_image.h&quot;
#include &quot;FileIO.h&quot;

The problem says Surface::m_mesh and Surface::m_program are undefined classes 'Mesh' and 'Program' but actually I added them as can be seen in the Model.cpp. Why do I encounter with the this problem?

I have examined the preprocessor file of Model.cpp and there is nothing wrong.

答案1

得分: 1

被用作变量声明的类型说明符,类声明必须是完整的。

非静态数据成员不能具有不完整的类型。是类的静态数据成员可能具有不完整的类型。

至少在类Surface内部

class Surface
{
//...
private:
    Mesh m_mesh;
//...

你声明了类Mesh的数据成员是不完整的。因此,编译器会报错。

你需要在头文件Model.h中包含头文件BaseModel.h

同样,对于使用未定义或不完整的类的其他引用,例如类Program,你应该采取相同的做法。

至少,你应该在模块Model.cpp中交换头文件的包含顺序

#include "BaseModel.h"
#include "Model.h"
英文:

To be used as a type specifier of a variable declaration a class declaration shall be complete.

Non-static data members may not have an incomplete type. It is static data members of a class that may have an incomplete type.

At least within the class Surface

class Surface
{
//...
private:
Mesh m_mesh;
//...

you declared a data member of the class Mesh that is an incomplete. So the compiler issues an error.

You need to include the header BaseModel.h in the header Model.h

The same you should do relative to other references to using undefined or incomplete classes as for example the class Program

At least you should exchange inclusions of the headers

#include &quot;Model.h&quot;
#include &quot;BaseModel.h&quot;

like

#include &quot;BaseModel.h&quot;
#include &quot;Model.h&quot;

within the module Model.cpp

huangapple
  • 本文由 发表于 2023年5月13日 17:45:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定