简体   繁体   English

C ++编译错误

[英]c++ compilation error

i got a compile error which i do not understand. 我收到了一个我不理解的编译错误。 i have ah/cpp file combination that does not contain a class but just defines some utility functions. 我有ah / cpp文件组合,其中不包含类,而仅定义了一些实用程序功能。 when i try to use a struct that is defined in another class i get the error: 当我尝试使用另一个类中定义的结构时,出现错误:

error C2027: use of undefined type 'B::C' 错误C2027:使用未定义类型'B :: C'

so, stripped down to the problem, the h-file looks like this 因此,简单地解决了这个问题,h文件看起来像这样

namespace A {
    void foo(B::C::SStruct const & Var);
}

the definition of SStruct is in a class which is in another h-file, that is of course included. SStruct的定义在另一个h文件中的类中,该文件当然也包含在内。

namespace B {
    class C {
        public:
        struct SStruct { };
    };
}

the strange thing is, i can use this struct in other classes fine, it just seems to be related to this one h-file which contains just utility functions. 奇怪的是,我可以在其他类中很好地使用此结构,这似乎与仅包含实用程序功能的一个h文件有关。 what am i missing here? 我在这里想念什么? thanks! 谢谢!

After correcting missing semicolons etc. this compiles: 更正丢失的分号等后,将编译:

namespace B {
    class C {
        public:
        struct SStruct { };
    };
}
namespace A {
    void foo(B::C::SStruct const & Var);
}

Obviously, if the order of the two namespaces were switched, this would not work. 显然,如果切换了两个名称空间的顺序,则将无法正常工作。 Possibly you are #including your headers in the wrong order. 可能是您#错误地包含了标题。 If this is the error, that's bad design - you should not allow header order to matter in your code. 如果这是错误,那是错误的设计-您不应允许标头顺序在您的代码中起作用。

I assume you meant "class C", not "Class C". 我假设您的意思是“ C类”,而不是“ C类”。

struct SStruct is private to class C (private being the default visibility of class members). struct SStruct对C类是私有的(private是类成员的默认可见性)。

As such, it is not visible outside class C and its friends, which does not include A::foo(). 因此,它在类C及其朋友之外不可见,该类不包括A :: foo()。

Class C { 
        struct SStruct { }; 
    } 

==> ==>

class C { 
public:
        struct SStruct { }; 
    };

Your class is missing a semicolon after the definition. 您的班级在定义后缺少分号。 It should be: 它应该是:

namespace B {
    class C {
        public:
        struct SStruct { };
    }; // <=== Here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM