-3

so at the moment I am trying to run a method within a <translator> class, by passing it an instance of a <bintree> class from my main.cpp. The following is my code, with the error that I am recieveing on the bottom. Im sure I am just missing some aspect to passing parameters, but for the life of me I cannot figure it out.

main.cpp (area where it creates bintree and where it is passed) bottom line most relevant

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.
     }
  }

I am receiving it through translator.h (edit: it knows the consts for 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 is not mine, it was provided. the starting declarations as follows. It is very long so and the functions themselves are not important for this issue, so i wont include them.

#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:
       //....
    };
}

and the errors i receive are:

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)

thank you in advance to anyone who can at least point me in the right direction :)

  • http://stackoverflow.com/questions/553682/when-can-i-use-a-forward-declaration (forward declaration) – Marian Munteanu Jun 01 '16 at 07:51
  • Don't put other includes inside the `namespace {` ; if you want stuff to be in a namespace then put the appropriate namespace definitions inside the other header – M.M Jun 01 '16 at 07:55
  • Where is morseNode defined? – thorsan Jun 01 '16 at 07:55
  • 2
    `translator.h` needs to include `bintree.h` and refer to the type as `treespc::bintree` – M.M Jun 01 '16 at 07:56
  • class `bintree` is inside a namespace `treespc`, so it should be ` void morseToEngTranslation(list &myList, treespc::bintree &myTree)` – Karsten Koop Jun 01 '16 at 07:56
  • @thorsan it is defined in a separate .h file as a struct. translator.h knows of it because main has them included with an #include "structs.h" line. – Mark Macdonald Jun 01 '16 at 07:58
  • @MarkMacdonald, but shouldnt it then be included in the translator.h file? – thorsan Jun 01 '16 at 07:59
  • @thorsan if i do `#include "structs.h"` within translator.h as well as man.cpp then i get a redefinition error when compiling. So it is reading the structs. – Mark Macdonald Jun 01 '16 at 08:02
  • @MarkMacdonald, not in the example translator.h above. You should include it where you use it, not assume it has been included before. – thorsan Jun 01 '16 at 08:04
  • @MarkMacdonald getting a redefinition error in that situation means you have some other problems with your headers that need fixing (perhaps along the lines of namespace usage that I mentioned either) – M.M Jun 01 '16 at 08:09

2 Answers2

2

Give the namespace for bintree, either using using namespace treespec or treespc::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
thorsan
  • 1,034
  • 8
  • 19
  • This is absolutely correct. Credit to @M.M who first mentioned it in a comment to the post. But thank you thorsan for detailing it out. I was missing the treespc namespace ! Thank you all so very much, I feel silly for missing that :P – Mark Macdonald Jun 01 '16 at 08:01
-1
ifndef BINTREE_H_
#define BINTREE_H_

Are you missing a # here?

UPDATE: You must include bintree header or use forward declaration (be careful as your class is inside the namespace) See answers here :Why can't I forward-declare a class in a namespace like this?

Community
  • 1
  • 1
vcp
  • 962
  • 7
  • 15