请求 [..] 中的成员 [..],该成员属于非类类型 [..]

Request for member [...] in [...], which is of non-class type [...]

本文关键字:成员 类型 属于 请求      更新时间:2023-10-16

我已经在互联网上搜索了这个编译错误的解决方案。他们中的大多数似乎与两个选项有关:

  • 将指针视为对象,而不是取消引用它。
  • 将变量与返回变量类型的函数误认为并尝试访问它。

我的代码是:

#ifndef ESCRITOR_PROLOG_HH
#define ESCRITOR_PROLOG_HH
#include "planning.hh"
#include <string>
#include <ostream>
class EscritorProlog {
private:
  Planning planningAEscriure;
public:
  EscritorProlog(Planning planning);
  EscritorProlog(Planning &&planning);
  void escriuFitxerProlog(std::ostream &escritor);
};
#endif

main.cc:

#ifndef MAIN_CC
#define MAIN_CC
#include "escritorProlog.hh"
#include <iostream>
#include <fstream>
int main () {
  std::ofstream escritor("./test.txt");
  EscritorProlog test(Planning());
  test.escriuFitxerProlog(escritor);
}
#endif

如果您尝试使用

g++ -c main.cc -std=c++11

你只会得到错误...

main.cc: In function ‘int main()’:
main.cc:11:8: error: request for member ‘escriuFitxerProlog’ in ‘test’, which is of non-class type ‘EscritorProlog(Planning (*)())’
   test.escriuFitxerProlog(escritor); 

现在我不知道我是否遗漏了一些东西,但我在我的代码中看不到之前提到的两个问题中的任何一个。如果我公然失明或其他任何东西,我很抱歉,但我只是没有看到错误。

任何帮助将不胜感激。

这是最令人烦恼的解析实例。

您可以使用列表初始化来修复它,这在以下上下文中是明确的:

EscritorProlog test(Planning{});

不幸的是,您有来自Planning的模棱两可的EscritorProlog结构:

EscritorProlog(Planning planning); // (1)
EscritorProlog(Planning &&planning); // (2)

假设您要为 (1) 定义一个复制构造函数,请将签名更改为

EscritorProlog(const Planning& planning);

以避免歧义。通过这些更改,您的代码将进行编译。

魔杖盒示例