尝试从本地类方法访问属性时出现编译错误

问题描述:

我试图从“内部”(本地)类的方法访问“外部”类的属性,但是失败。尝试从本地类方法访问属性时出现编译错误

这无法编译

class outer 
{ 
    public: 
     std::string name; 

     class inner 
     { 
      public: 
      void hello(); 
     }; 

     void dostuff(); 

}; 

void outer::inner::hello(){std::cout << "Hello " << name << "\n";} 

void outer::dostuff(){inner instanceInner; instanceInner.hello();} 


int main() 
{ 
    outer instanceOuter; 
    instanceOuter.name = std::string("Alice"); 
    instanceOuter.dostuff(); 

    return 0; 
} 

编译错误:

9:21: error: invalid use of non-static data member 'outer::name' 
21:53: error: from this location 

我真的不希望name为静态成员,但我真的不介意我的特定目的outer是一个单身人士。于是,我试着用static std::string name;并获得

编译错误:

/tmp/ccqVKxC4.o: In function `outer::inner::hello()': 
:(.text+0x4b): undefined reference to `outer::name' 
/tmp/ccqVKxC4.o: In function `main': 
:(.text.startup+0x1f): undefined reference to `outer::name' 
collect2: error: ld returned 1 exit status 

你能帮助我吗?

您的问题出在您的hello()函数。 name超出范围在这里。它不是你的inner课程的一部分。不幸的是你的内部类不会有你的外部类及其成员的可见性,从而这样的:

void outer::inner::hello(){ 
    std::cout << "Hello " << name << "\n"; 
} 

将产生一个错误,告诉你name无法找到。

你可以做到以下几点:

#include <iostream> 
#include <string> 

class outer 
{ 
    public: 
     static std::string name; 

     class inner 
     { 
      public: 
      void hello(); 
     }; 

     void dostuff(); 

}; 


std::string outer::name = ""; // This is key. You need to instantiate outer's name somewhere. 

void outer::inner::hello(){std::cout << "Hello " << outer::name << "\n";} 

void outer::dostuff(){inner instanceInner; instanceInner.hello();} 


int main() 
{ 
    outer instanceOuter; 
    instanceOuter.name = std::string("Alice"); 
    instanceOuter.dostuff(); 

    return 0; 
} 

输出:

Hello Alice 
+0

感谢您的回答。我看不出你的“hello”和我的区别(void outer :: inner :: hello(){std :: cout

+0

我只是指出你的错误谎言和为什么发生。如果你现在检查我的编辑,你可以看到一个建议来管理这@ @ Remi.b – 0xDEFACED

+0

我可以传递'name'给函数。我试图不通过它。原因是我有一个相对较长的代码(约1000行),我只是将整个想法包裹在一个外部类中。我希望不必将外部类的每个属性都传递给每个函数调用。请注意,将所有参数作为参数传递最终可能会降低性能,对于那些可能有5个参数作为参考传递的小函数,这些小函数也会被重复调用。否则最终可能无法做到(除非通过外部类的实例调用每个属性) –

重复(带小的修改)的东西,我在另一个答复中提到,到similar recent SO question

A C++ nested class does not share data with its outer class -- if there were the case, each instance of the inner class would be in a one-to-one relationship with a corresponding instance of an outer class and would thus add no extra functionality. Instead, the main purposes of nested classes are:

  1. Namespace meaning: outer::inner is more meaningful than outer_inner
  2. Depending on the C++ version standard, a nested class has extra visibility of the member objects of instances of the outer class
  3. Maybe others that I'm not aware of

这是一个受欢迎的参考就当/为什么在C使用嵌套类++ erence问题:Why would one use nested classes in C++?

在你的情况下,inner类不能引用数据name没有任何一)outer类,其name的具体实例将访问,或b )通用于outer的静态name。第一种解决方案需要inner类与一种outer类的引用来构造:

inner (const outer& _o) : o(_o) {}

其中oconst outer&类型的私有成员。然后hello功能写入

void outer::inner::hello(){std::cout << "Hello " << o.name << "\n";} 

另外,如果你想name一成不变的,它是一个未定义的引用,因为你必须把

std::string outer::name = "bla"; 

编译单元中的某处,否则静态成员没有定义。

不管怎样,我都担心你滥用嵌套类。它在你的代码中有什么用途?为什么数据和成员函数必须在外部类和嵌套类之间分开?你确定一个班级不会更好地为你的目的服务吗?