gpt4 book ai didi

c++如何声明返回对象 vector 的函数

转载 作者:行者123 更新时间:2023-11-28 07:16:23 25 4
gpt4 key购买 nike

我在函数中挣扎,它应该返回对象的 vector ,但由于某种原因它总是抛出错误,告诉我我的对象是未声明的标识符,这个对象的 vector 不是有效的模板,并指向我所在的 .h 文件声明功能。我将适本地解释这意味着什么以及如何解决这个问题。在下面,我放置了我的类(class)和起始文件中的代码。

#ifndef SETUPW_H
#define SETUPW_H
#include"Square.h"
#include <iostream>
#include<string>
#include<fstream>
#include<vector>

std::vector<std::ifstream> allText();
std::ifstream loadTxt(std::string txt);
void printByLine(std::ifstream& txt);
std::vector<square> allSquares();//compiler points me to this line and that one bellow
void whichSQ(int sqNum, std::vector<square> sq);

#endif

和我的类(class):

#ifndef SQUARE_H
#define SQUARE_H
#include"player.h"
#include"setupW.h"
#include<iostream>
#include<string>
#include<fstream>

class square
{
public:
square(std::string name, int sqNumber, std::string description, int exits, int object);
void loadSQ(std::ifstream& inFile);
void printSQ();

private:
int mSqNumber;
std::string mName;
std::string mDescription;
int mExits;
int mObject;
};

#endif

最佳答案

问题的出现是因为你在这里有一个循环依赖。在 square.cpp你首先包括square.h .但是square.h包含此行 #include"setupW.h" (在你的类(class)声明之前)。因此,您的函数声明将出现在您的 square 声明之前。类(class)。这导致编译器喃喃自语 square当他阅读 std::vector<square> 时(当时)没有宣布.

最简单的解决方案是简单地删除包含,因为据我所知,这是不必要的。

关于c++如何声明返回对象 vector 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20174428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com