简体   繁体   English

未知的返回类型错误(C ++)

[英]Unknown return type error (C++)

My problem is that i need to first define my interface and then implement it further in code but my problem i that function that should return type that is known inside class seems to be unknown outside class when i implement method. 我的问题是我需要首先定义我的接口,然后在代码中进一步实现它,但我的问题是,当我实现方法时,应该返回类内部已知的类型的函数似乎是未知的类。

This is my code: 这是我的代码:

class Test {
    class Inner {
    };    
public:    
    Inner* foo (void);
};

Inner* Test::foo(){
}

This code produce error because type Inner is unknown for the function outside the class. 此代码产生错误,因为类型Inner对于类外部的函数是未知的。 Can anyone help me how to make simple function that would return type defined only inside class ? 任何人都可以帮助我如何创建只返回类内部定义的类型的简单函数?

Thank you for any help. 感谢您的任何帮助。

You need 你需要

Test::Inner* Test::foo(){

} }

If a member function definition is outside the class definition, then the return type is not in the class scope, unlike the rest of the function, so you need to explicitly qualify the return type. 如果成员函数定义在类定义之外,那么返回类型不在类范围内,与函数的其余部分不同,因此您需要显式限定返回类型。

Since no one has mentioned it, you can also do this in C++11: 既然没人提到它,你也可以在C ++ 11中做到这一点:

auto Test::foo() -> Inner * {...}

This can be useful if the fully qualified name is long. 如果完全限定名称很长,这可能很有用。 In C++14, you can leave off the trailing type part and just do: 在C ++ 14中,您可以不使用尾随类型部分,只需执行以下操作:

auto Test::foo() {...}

This will deduce the return type. 这将推断出返回类型。

Inner is a nested class and outside class Test it has to be fully qualified: 内蒙古是一个嵌套类和课外Test它必须是完全合格的:

Test::Inner* Test::foo() {
    //...
}

because in global scope Inner is indeed unknown, only Test::Inner , so a Inner inside Test is known. 因为在全球范围Inner确实是未知的,只有Test::Inner ,所以InnerTest是已知的。 You could also have another Inner in global scope, just the same as with Test , and this will be other Inner , not Test::Inner . 你还可以在全局范围内拥有另一个Inner ,与Test相同,这将是其他Inner ,而不是Test::Inner

You does not seem to specify the scope so it, of course, remains unknown. 您似乎没有指定范围,因此它当然仍然未知。 The C++ compiler will look for an Inner class outside your Test class which could also present as a different class, but it is not in your particular case. C ++编译器将在Test类之外寻找一个Inner类,它也可以作为一个不同的类出现,但它并不在你的特定情况下。

That is why you will need to also provide the scope, even for the return type. 这就是为什么你还需要提供范围,即使是返回类型。 That is not to say you would need to use the scope inside your test class, but outside, you will have to due to that. 这并不是说您需要在测试类中使用范围,但在外部,您将不得不使用它。

Thereby, the correct code would be something like this: 因此,正确的代码将是这样的:

class Test {
    class Inner {
    };    
public:    
    Inner* foo (void);
};

Test::Inner* Test::foo(){
//^^^^
}

Strictly speaking, if you have a recent compiler, you could even use auto, but then it gets a bit less comprehensive. 严格地说,如果你有一个最近的编译器,你甚至可以使用auto,但它会变得不那么全面。

class Test {
class Inner {
};     
public:    
    Inner* foo (void);
};

Test::Inner* Test::foo(){
}

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

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