【问题标题】:c++ 'class' has not been declared - passing a class to a classc ++'class'尚未声明-将类传递给类
【发布时间】:2016-09-30 11:00:45
【问题描述】:

所以目前我正在尝试在 <translator> 类中运行一个方法,方法是从我的 main.cpp 向它传递一个 <bintree> 类的实例。以下是我的代码,我在底部收到错误。我确定我只是错过了传递参数的某些方面,但对于我的生活,我无法弄清楚。

main.cpp(它创建 bintree 和传递它的区域)最相关的底线

if (validFile == true)
{
  //Create bintree through insert. Rebalance follows
  bintree<morseNode> morseTree;
  for (int count = 0; count < 26; count++)
  {
     char letter = morseCodes[count].letter;
     string code = morseCodes[count].code;
     morseNode node;
     node.letter = letter;
     node.code = code;
     morseTree.insert(node);
  }
  morseTree.rebalance();     
  translator fileTranslator(outputFile);//create instance of translator

  //Read and translate files based on conversion type
  if (translatorType != "e" || translatorType != "E") //English -> Morse Conversion
  {
     validFile = readFile(inputFile, translatorType, morseCodes, inputList);
     if (validFile == true)
     {
        fileTranslator.engToMorseTranslation(inputList, morseCodes);
     }
  }
  else //Morse -> English Conversion
  {
     validFile = readFile(inputFile, translatorType, morseCodes, inputList);
     if (validFile == true)
     {
        fileTranslator.morseToEngTranslation(inputList, morseTree);
        //Here is where it sends morseTree that is throwing ^^ the error.
     }
  }

我通过 translate.h 接收它(编辑:它知道 morseNode 的常量)

#ifndef TRANSLATOR_H
#define TRANSLATOR_H

#include <string>
#include <iostream>
#include <list>

//I tried #include "bintree.h" here. this did not work

using namespace std;

class translator
{
private:
  string outName;
  list<char> morseOutput;
public:
  void morseToEngTranslation(list<char> &myList,  bintree<morseNode> &myTree)
{
    //functions here.. seemed irrelevant as i just wanted to show how i am
    //receiving the parameters
}
};
#endif

bintree 不是我的,它是提供的。起始声明如下。太长了,函数本身对这个问题并不重要,所以我不会包括它们。

#ifndef BINTREE_H_
#define BINTREE_H_

#include <stdexcept>

namespace treespc
{
   // forward class declaration
   template <typename dataType> class bintree;
   template <typename dataType> class binnode;

   #include "const_iterator.h"
   #include "binnode.h"

   /********************************************************\
      template class for a binary tree
   \********************************************************/

   template <typename dataType> class bintree
   {
       public:
       //....

       private:
       //....
    };
}

我收到的错误是:

translator.h:79:52: error: ‘bintree’ has not been declared
    void morseToEngTranslation(list<char> &myList,  bintree<morseNode> &myTree)



translator.h:79:59: error: expected ‘,’ or ‘...’ before ‘<’ token
    void morseToEngTranslation(list<char> &myList,  bintree<morseNode> &myTree)

提前感谢任何至少可以为我指明正确方向的人:)

【问题讨论】:

  • 不要在 namespace { 中添加其他包含;如果您希望将内容放在命名空间中,则将适当的命名空间定义放在另一个标题中
  • morseNode 定义在哪里?
  • translator.h 需要包含bintree.h 并将类型称为treespc::bintree
  • class bintree 在命名空间 treespc 内,所以它应该是 `void morseToEngTranslation(list &myList, treespc::bintree &myTree)`

标签: c++ class parameters compiler-errors


【解决方案1】:

为 bintree 提供命名空间,使用 using namespace treespectreespc::bintree

#ifndef TRANSLATOR_H
#define TRANSLATOR_H

#include <string>
#include <iostream>
#include <list>

#include "bintree.h"

using namespace std;

class translator
{
private:
  string outName;
  list<char> morseOutput;
public:
  void morseToEngTranslation(list<char> &myList,  treespc::bintree<morseNode> &myTree)
{
    //functions here.. seemed irrelevant as i just wanted to show how i am
    //receiving the parameters
}
};
#endif

【讨论】:

  • 这完全正确。感谢@MM,他在帖子的评论中首先提到了它。但感谢托尔桑详细说明。我错过了 treespc 命名空间!非常感谢你们,错过了我觉得很傻:P
【解决方案2】:
ifndef BINTREE_H_
#define BINTREE_H_

您是否在这里缺少#

更新:您必须包含 bintree 标头或使用前向声明(请小心,因为您的类在命名空间内)在此处查看答案:Why can't I forward-declare a class in a namespace like this?

【讨论】:

  • 是的,抱歉,它在程序中。只是糟糕的复制和粘贴。修好了。
猜你喜欢
  • 1970-01-01
  • 2017-10-24
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多