要求[...]成员[...],这是非类型[...]

时间:2017-02-01 23:23:37

标签: c++ compiler-errors

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

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

我的代码是:

#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); 

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

这是the most vexing parse的实例。

您可以使用 list-initialization 修复它,这在此上下文中是明确的:

EscritorProlog test(Planning{});

很遗憾,您EscritorProlog的构造不明确Planning

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

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

EscritorProlog(const Planning& planning);

为了避免歧义。通过这些更改,您的代码将被编译。

wandbox example