Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Class Typedef Struct does not name a type

Tags:

c++

templates

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?

like image 345
Nexus Avatar asked May 29 '12 14:05

Nexus


2 Answers

"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;
}
like image 116
Viktor Latypov Avatar answered Oct 13 '22 00:10

Viktor Latypov


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.

like image 45
juanchopanza Avatar answered Oct 13 '22 00:10

juanchopanza