I'm trying to make use of typedef structs inside of my C++ program. I started writing the following code until I received an error when trying to add a method to my class that returns a template typedef struct pointer.
StructSource.h
template <typename T>
class StructSource {
public:
struct TestStruct{
T value;
};
};
User.h
#include "StructSource.h"
class User {
public:
typedef StructSource<int>::TestStruct IntStruct;
IntStruct *getIntStruct();
};
User.cpp
#include "User.h"
IntStruct *User::getIntStruct() {
return 0;
}
This gives the following error when compiling with GCC.
User.cpp:3:1: error: ‘IntStruct’ does not name a type
I'm at a loss to explain why this is the case. What type information am I missing?
"User" is also a "namespace" (scope, actually, as most commenters point out - "namespace" was for a quick answer) here, so you have to use
User::IntStruct *User::getIntStruct() {
return 0;
}
You need:
User::IntStruct *User::getIntStruct() { ... }
IntStruct is defined inside the User
scope, but the return value of the function is not in scope. there is a discussion of these scoping issues here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With