1

I have the following issue thrown by the compiler:

include/FlowChannel.h:14:21: error: ‘LatticeCell’ was not declared in this scope std::vector grid;

when having these 3 header files (LatticeCell.h, FlowChannel.h and Utilities.h) and 2 cpp files including them(lbm.cpp and Utilities.cpp):

LatticeCell.h

#ifndef LATTICECELL_H
#define LATTICECELL_H

#include <iostream>
#include <vector>
#include <string>
#include <cmath>

/* Single cell */
using namespace std;
class LatticeCell{
    private: 

        std::vector<double> matrix = {0,0,0,0,0,0,0,0,0}; 
        unsigned int type;   //fluid, no-slip, velocity or density 

    public: 
    
        //Constructor
        LatticeCell(unsigned int inType){
            type = inType; 
        }
};
#endif

FlowChannel.h

#ifndef FLOWCHANNEL_H
#define FLOWCHANNEL_H

#include <vector>
#include <string>
#include <cmath>
#include <iostream>
#include "LatticeCell.h"

using namespace std;

class FlowChannel{
    private: 
        std::vector<LatticeCell> grid;            //ERROR LINE
        unsigned int dimX = -1; 
        unsigned int dimY = -1; 

    public: 
        FlowChannel(unsigned int nx, unsigned int ny){
            dimX = nx+2;
            dimY = ny+2;
            unsigned int gridSize = dimX*dimY; 
            grid.reserve(gridSize);
            initGrid(/*TODO Params*/); 
        }
};
#endif

lbm.cpp

#include <string>
#include <vector>

#include "LatticeCell.h"
#include "FlowChannel.h"
#include "Utilities.h"

int main(int argc, char** argv){
    printsomething();
    return 0; 
}

Utilities.cpp

#include "LatticeCell.h"
#include "FlowChannel.h"


#include <string>
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

void printsomething(){
    cout << "something" << std::endl; 
}

double calcRelaxationTime(unsigned int ny , double reynolds, double uin){
    return 3.0 * (uin * ny / reynolds) - 0.5;
}

Utilities.h

#ifndef UTILITIES_H
#define UTILITIES_H


#include "LatticeCell.h"
#include "FlowChannel.h"
#include <vector>
#include <cmath>

void printsomething(); 

#endif

Further my compiler flags are:

-Wall -std=c++17 -pedantic 

For some reason I can't figure out, why LatticeCell wouldnt be a declared class in FlowChannel, due to it being included. Do you guys know whats wrong?

Edit: I added lbm.cpp, Utilities.cpp and Utilities.h so you guys see the full scope of the problem

  • Copied the code you've shown to two header files. Cannot reproduce the symptom you describe. Only error I get is related to `initGrid()` being called but not defined, which is obvious on inspection of the code shown. You are probably getting the error you describe in a source file that attempts to use `LatticeCell`, but has not included either of your headers. – Peter Jul 01 '20 at 14:02
  • [Never ever _EVER_ put `using namespace std;` in a header](https://stackoverflow.com/q/1452721/501250) -- and you shouldn't use it anywhere else, either. – cdhowie Jul 01 '20 at 14:16
  • yes sry about the namespace was one of my desperate attempts to find a soloution to this problem. Is there a reason you shouldnt use it in general though? – Florian Kloeppner Jul 01 '20 at 14:29
  • And sorry to hear that you couldnt reproduce the issue.. weird but thanks for trying! – Florian Kloeppner Jul 01 '20 at 14:29
  • Look for #include cycles. Often they lead to “unknown symbol” or “incomplete type” compilation errors. – besc Jul 01 '20 at 15:17
  • @cdhowie I use namespace std in most of my files, it saves me a lot of time when accessing methods so I do not need to type std::cout std::end and etc, I have rarely had a problem with it, I mean if you are just starting out using namespace std; is a great way to start, once you get more advanced and define your own namespaces than you can take it out.... – Yunfei Chen Jul 01 '20 at 19:04
  • @YunfeiChen That's not the reason you shouldn't use it. Read the answers on the question I linked. – cdhowie Jul 01 '20 at 21:24

2 Answers2

0

You should check if the files are in the same directory. I copy and paste your code in VS 2019 and it work for me, here are the pictures FlowChannel LatticeCell

0

It seems, that deleting #include 'LatticeCell.h' everywhere but in FlowChannel.h. I dont get the error 100% to be honest, as this wouldn't execatly cause an include loop that would induce such an error, but it works.