2

I am still fairly new to c++ so please allow! Essentially I have created a struct in the header file for one of my classes full of strings.

typedef struct details{
        string name;
        string address;
}details;

and I wish to not only use this struct in the cpp file that belongs to the header, but in other classes as well. For example, I want to create a vector of this struct in another header file.

private:
vector <details> foo;

I also want to use the struct in the main

details d;
d.name = "hi";
d.address = "hello";

however when I currently try to do this I get errors such as

error: 'details' was not declared in this scope
     vector <details> foo;

and

error: template argument 1 is invalid
     vector <details> foo;

has anyone had similar issues who can provide insite into what I can do to fix this? Thanks a lot.

EDIT TO DEMONSTRATE CODE

class1.h

#include "class2.h"

struct trans1{
    string name;
};
class class1 {

private:
    vector <trans2> t2;

public:
    class1();
};

class2.h

#include "class1.h"

struct trans2{
    string type;
};

class class2{

private:
    vector <trans1> t1;

public:
    class2();
};

errorlog:

In file included from class1.h:3:0,
                 from class1.cpp:1:
class2.h:21:13: error: 'trans1' was not declared in this scope
     vector <trans1> t1;
             ^
class2.h:21:19: error: template argument 1 is invalid
     vector <trans1> t1;
                   ^
class2.h:21:19: error: template argument 2 is invalid

I understand that this is ridiculous code in a real world application however this is the simplest way I could demonstrate

3
  • 3
    You have to include this header in each cpp module that refers to the structure. Commented Mar 22, 2015 at 13:53
  • 1
    Please provide an SSCCE. Commented Mar 22, 2015 at 13:58
  • Ive added code above Commented Mar 22, 2015 at 18:51

3 Answers 3

3

You have to include the header file that contains the definition of the struct everywhere you use it.

The only cases where you don't have to is when you are only declaring references or pointers to it; in that case you can just forward declare it with:

struct details;

Also in C++ you can just declare it with:

struct details{
    std::string name;
    std::string address;
};

There's really no need for the typedef.

Sign up to request clarification or add additional context in comments.

2 Comments

@πάνταῥεῖ > The only cases where you don't have to is when you are only declaring references or pointers to it; in that case you can just forward declare it
@Jefffrey: unique_ptr, too. But not auto_ptr, for example.
2

details.h

#include <string>

#ifndef DETAILS_H
#define DETAILS_H

struct details {
  std::string name;
  std::string address;
};

#endif

details.cpp

#include "details.h"
//insert implementation here

other_header.cpp

#include "details.h"
#include <vector>

std::vector<details> main_use;
//whatever else here

This should work.

EDIT

If you want to use it in another class:

my_class.h

#include "details.h"
#include <vector>

#ifndef MYCLASS_H
#define MYCLASS_H

class myClass {
std::vector<details> class_use;
//insert stuff here

};

#endif

EDIT 2

I'm pretty unsure why you define the structs in the classes as you do -- it is kind of confusing. Here's how I would do if. Note the #ifndef ... #define ... #endif pattern is very important. It's also a bad idea to include headers that include themselves. I would organize your code (as you have it) in the following way:

trans.h

#ifndef TRANS_H 
#define TRANS_H

#include <string>
struct trans1 {
   std::string name;
};

struct trans2 {
   std::string type;
};

#endif

class1.h

 #ifndef CLASS1_H
 #define CLASS1_H

 #include "trans.h"
 #include <vector>

 class Class1 {
   public:
     Class1();
   private:
     std::vector<trans2> t2;
 };

 #endif

class2.h

 #ifndef CLASS2_H
 #define CLASS2_H

 #include "trans.h"
 #include <vector>

 class Class2 {
   public:
     Class2();
   private:
     std::vector<trans1> t1;
 };

 #endif

Now the way it's organized, you can use the trans struct by #include "trans.h"'ing in the main, along with eliminating the circular includes. Hope this helped.

You'll find that main.cc below compiles without error now.

main.cc

#include<iostream>
#include "class1.h"
#include "class2.h"
#include "trans.h"

int main()
{
    std::cout << "Hello, World!" << std::endl;

    return 0;

}

erip

5 Comments

I have included the header files but have received a different error: In file included from class.h:3:0, library.h:22:13: error: 'newClass' was not declared in this scope vector <newClass> nC;
So the same error but this time with a class. I have included all headers in every class. I am not sure where I am going wrong.
You said you need a vector of structs, not a vector of classes. I'm confused.
Yes I also have a vector of type class, which throws the same error even with the header files in the correct position.
Post the minimum code needed to recreate this error in an edit.. I'm not understanding.
0

The struct definition in c++ looks like

 #include <string>

 struct details{
    std::string name;
    std::string address;
 };

and must be seen before it's used elsewhere in your code. Assumed you placed the declaration from above in a header file details.hpp, you should have the following to use it

 #include <vector>
 #include "details.hpp"

 // Some context

 std::vector<details> vdetails;

In certain situations, when the details struct members aren't actually accessed, you can also use a forward declaration as

 struct details;

instead of in including the complete struct declaration. This can be used then, to declare details* pointers or details& references in further declarations, as long there's nothing dereferenced with them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.