简体   繁体   English

在类中声明结构

[英]Declaring a struct in a class

I created a class called Text , and one of its members is a std::vector of Pal_freq , a struct declared inside the class . 我创建了一个名为Text的类,其成员之一是Pal_freqstd::vector ,它是在class内部声明的struct

I am getting those errors: 我收到那些错误:

error: 'Pal_freq' does not name a type bool criteri (const Pal_freq& p1, const Pal_freq& p2); 错误:“ Pal_freq”未命名布尔型Criteri类型(const Pal_freq&p1,const Pal_freq&p2);

error: 'Pal_freq' does not name a type bool criteri (const Pal_freq& p1, const Pal_freq& p2); 错误:“ Pal_freq”未命名布尔型Criteri类型(const Pal_freq&p1,const Pal_freq&p2);

error: 'Pal_freq' was not declared in this scope vector consultar_taula_freq(); 错误:未在此范围向量Consultar_taula_freq()中声明“ Pal_freq”;

error: template argument 1 is invalid vector consultar_taula_freq(); 错误:模板参数1为无效向量Consultar_taula_freq();

Here is the code: 这是代码:

class Text {
 public:

  struct Pal_freq {
  int freq;
  string pal;
  };

  vector <Pal_freq> taula;
};

The error messages are not coming from the tiny piece of ode you showed (the definition of the struct), they are coming from other pieces of code that you didn't show us (ie where you try to use the nested struct). 错误消息不是来自您所展示的一小段颂歌(结构的定义),而是来自您向我们展示的其他代码段(即,您尝试在何处使用嵌套结构)。

Because it is declared inside Text you need to qualify its name when you refer to it outside the scope of Text , so you need to refer to it as Text::Pal_freq eg 因为它是在Text内部声明的,所以在Text范围之外引用它时,需要限定其名称,因此需要将其引用为Text::Pal_freq例如

bool criteri (const Text::Pal_freq& p1, const Text::Pal_freq& p2);

Please read the "how to ask" guidelines on the site, and make sure you include all the necessary code for us to help you. 请阅读网站上的“如何提问”指南,并确保您包含所有必要的代码以帮助我们为您提供帮助。

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

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