C ++类Typedef Struct不命名类型 [英] C++ Class Typedef Struct does not name a type

查看:945
本文介绍了C ++类Typedef Struct不命名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的C ++程序中使用typedef结构体。我开始写下面的代码,直到我收到一个错误,当试图添加一个方法返回一个模板typedef结构指针的类。

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

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;
}

在使用GCC编译时会出现以下错误。

This gives the following error when compiling with GCC.


User.cpp:3:1:错误:'IntStruct'未命名类型

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?

推荐答案

用户也是一个命名空间 out - namespace是一个快速的答案),所以你必须使用

"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;
}

这篇关于C ++类Typedef Struct不命名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆