简体   繁体   中英

c++ how declare function returning vector of objects

and I struggle with function which should return vector of objects but for some reason it throws errors all the time, telling that my object is undeclared identifier and vector of this objects is not valid template and points me to .h file where I declare function. I will appropriate any explanation what that mean and how to fix this. bellow I place code from my class and starting files.

#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

and my 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

The problem arises because you have a circular dependency here. In square.cpp you firstly include square.h . But square.h contains this line #include"setupW.h" (before your class declaration). Therefor the declarations of your functions will appear before the declaration of your square class. That causes the compiler to mutter that square is not declared (at that time) when he reads std::vector<square> .

The most easiest solution would be to simply remove the include, because it is, as far as I can tell, unneccessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM