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 :)
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
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?
Related
I am attempting to create a templated vector class, but upon compilation I am receiving an error of
def.hpp:3:1: error: 'TempVector' does not name a type
I keep referring to reference material and my syntax and handling of the header file declaration and definition (.h and .hpp) seem right to me, but I can not figure out what I am overlooking.
Below is the three files I am working with, thank you.
driver.cpp:
#include <iostream>
#include <string>
#include "dec.h"
using namespace std;
int main() {
TempVector <int> v1;
cout<<"ran successfully"<<endl;
}
dec.h:
#ifndef DEC_H
#define DEC_H
#include <iostream>
#include <utility>
// Declaration of class Vector
template <typename T>
class TempVector {
public:
TempVector ();
private:
T* array;
static const unsigned int spare = 10;
};
#include "def.hpp"
#endif
def.hpp:
template <typename T>
TempVector<T>::TempVector () {
std::cout<<"ran successfully";
}
So I have two classes - Dvd and DvdGroup. DvdGroup basically manages an array of dvds and provide manipulative member functions for that class. The problem is whenever I try to compile DvdGroup.cc using the command 'g++ -c Dvd.Group.cc', I get a bunch of errors all related to not having 'Dvd' declared and I'm not sure why.
Here are some errors below:
DvdGroup.h:14:12: error: ‘Dvd’ has not been declared void add(Dvd*);
DvdGroup.h:18:3: error: ‘Dvd’ does not name a type Dvd* dvdCollection[MAX_DVDS];
DvdGroup.cc: In copy constructor ‘DvdGroup::DvdGroup(DvdGroup&)’:
DvdGroup.cc:15:6: error: ‘Dvd’ was not declared in this scope for(Dvd d: dvds){
I feel like I'm missing something and they could all be fixed by one solution because they all involve having the Dvd class undeclared but I can't seem to figure out what. I was wondering if anyone could tell me what I'm doing wrong? I would really appreciate any help with fixing this.
DvdGroup.cc:
#include <iostream>
using namespace std;
#include "DvdGroup.h"
DvdGroup::DvdGroup(int n){
numDvds = n;
}
DvdGroup::DvdGroup(DvdGroup& dvds){
numDvds = dvds.numDvds;
for(Dvd d: dvds){
Dvd newDvd = Dvd;
}
}
DvdGroup::~DvdGroup(){
//code
}
void DvdGroup::add(Dvd* d){
//code
}
DvdGroup.h:
#ifndef DVDGROUP_H
#define DVDGROUP_H
#define MAX_DVDS 15
#include <string>
using namespace std;
class DvdGroup
{
public:
DvdGroup(int);
DvdGroup(DvdGroup&);
~DvdGroup();
void add(Dvd*);
private:
Dvd* dvdCollection[MAX_DVDS];
int numDvds;
};
#endif
Don't know if the Dvd header file is needed, but here:
Dvd.h:
#ifndef DVD_H
#define DVD_H
#define MAX_DVDS 15
#include <string>
class Dvd{
public:
Dvd(string, int);
void set(string, int);
Dvd(Dvd&);
int getYear();
~Dvd();
void print();
private:
string title;
int year;
};
#endif
What you need to do is to provide Dvd class definition for DvdGroup class. It is needed to know what type of symbol is this. Solution for your problem should be addition of:
#include "Dvd.h"
line to DvdGroup.h file.
I've created 2 header files. ListA.h and ListN.h
They both make their own use their own unique class List. When I compile my program (even though they have no way of knowing the other exists, it says the following error)
Im pretty sure it shouldnt be a redefinition, but it obviously is. Any help is appreciated.
ListA.h
#ifndef __LISTA_H_
#define __LISTA_H_
#include <iostream>
using namespace std;
class List{
public:
List(int = 0);
List(const List&);
~List();
};
#endif
ListN.h
#ifndef __LISTN_H_
#define __LISTN_H_
#include <iostream>
using namespace std;
class List{
public:
List(int = 10);
List(const List&);
~List();
};
#endif
ListA.cpp
#include "ListA.h"
using namespace std;
List::List(int mySize)
{
//...
}
ListN.cpp
#include "ListN.h"
#include <iostream>
using namespace std;
List::List(int size)
{
//...
}
Main
#include <iostream>
#include "ListN.h"
using namespace std;
int main()
{
List myList;
return 0;
}
Both cpp files are being compiled by the compiler. Thus, when the linker goes to link the files together, it gets confused, since there are multiple List classes.
To fix this, you could use namespaces, or you cold not expose at least one of the List classes.
Alternatively, if the idea was to be able to include ListN.h vs ListA.h for configuration purposes, this is the wrong way to do so. Either you should have a #define parameter for the header, or you should find some other way, such as through #ifdef. For example (I'm not 100% sure this would compile, but you get the idea):
List.h
#ifndef __LIST_H_
#define __LIST_H_
#ifndef LIST_PARAM
#define LIST_PARAM 0
#endif
#include <iostream>
using namespace std;
class List{
public:
List(int = LIST_PARAM);
List(const List&);
~List();
};
#endif
main.cpp
#include <iostream>
#define LIST_PARAM 10
#include "List.h"
using namespace std;
int main()
{
List myList;
return 0;
}
I personally don't like this method; it is much better to just pass the value in to the constructor:
int main()
{
List myList{ 10 };
return 0;
}
When linker trying to link find the definition / symbol for List, it does found in two different obj file and hence linker givers error. In visual studio error number : LNK2005
To solve this error, either:
To fix, add /FORCE:MULTIPLE to the linker command line options
Add the classes in two different namespaces which will avoid this error.
ListN.h
#ifndef __LIST_H_
#define __LIST_H_
#include <iostream>
using namespace std;
namespace ListN
{
class List{
public:
List(int = 10);
List(const List&);
};
}
#endif
ListN.cpp
#include "ListN.h"
#include <iostream>
using namespace std;
namespace ListN
{
List::List(int size)
{
//...
}
}
Main.cpp
#include <iostream>
#include "ListN.h"
int main()
{
ListN::List myList;
return 0;
}
I'm trying to pass a child object to a function that accepts its parent object with the code:
Rook bRookL();
board[0][0].setPieceObj(bRookL);
and I get the following errors:
ChessBoard.cpp:16:35: error: no matching function for call to 'Space::setPieceObj(Rook (&)())'
board[0][0].setPieceObj(bRookL);
^
In file included from ChessBoard.h:13:0,
from ChessBoard.cpp:5:
Space.h:62:10: note: candidate: void Space::setPieceObj(Piece&)
void setPieceObj(Piece &);
^
Space.h:62:10: note: no known conversion for argument 1 from 'Rook()' to 'Piece&'
Here is my code:
ChessBoard.h:
#ifndef CHESSBOARD_H
#define CHESSBOARD_H
#include "Space.h"
using namespace std;
class ChessBoard {
private:
Space board[8][8]; // board[0][0] is the upper-left corner of the board and
// board[7][7] is the lower-right corner of the board
public:
ChessBoard();
};
#endif /* CHESSBOARD_H */
ChessBoard.cpp:
#include "ChessBoard.h"
#include "Space.h"
#include "Rook.h"
using namespace std;
ChessBoard::ChessBoard() {
Rook bRookL();
board[0][0].setPieceObj(&bRookL);
}
Rook.h:
#ifndef ROOK_H
#define ROOK_H
#include "Piece.h"
using namespace std;
class Rook : public Piece {
public:
Rook() : Piece() {
}
};
#endif /* ROOK_H */
Piece.h:
#ifndef PIECE_H
#define PIECE_H
using namespace std;
class Piece {
public:
Piece() {
}
};
#endif /* PIECE_H */
Space.h:
#ifndef SPACE_H
#define SPACE_H
#include "Piece.h"
#include "Rook.h"
using namespace std;
class Space {
private:
Piece *pieceObj;
public:
void setPieceObj(Piece &);
};
#endif /* SPACE_H */
Space.cpp:
#include "Space.h"
using namespace std;
Space::Space() {
}
void Space::setPieceObj(Piece &p) {
pieceObj = p;
}
Please help. Thanks.
It appears, based on the examination of your code, that setPieceObj() should probably take a pointer argument:
void Space::setPieceObj(Piece *p) {
pieceObj = p;
}
That's one problem. Now, you need to pass a pointer to a Piece object.
Rook bRookL();
board[0][0].setPieceObj(&bRookL);
That's a most vexing parse, that declares a function that returns a Rook, and passes a pointer to it, to setPieceObj().
There's not enough information to determine your clear intent. Perhaps:
Rook bRook;
board[0][0].setPieceObj(&bRookL);
Rook bRookL();
board[0][0].setPieceObj(&bRookL);
1.Rook bRookL(); doesn't do what you expect, it's a declaration of function , which takes no parameters and returns Rook, i.e. Rook (*)() (just as compiler told you).
2.&bRookL will return the address of bRookL, so it will be a pointer. But Space::setPieceObj(Piece &p) expects its paramter to be Piece&. It seems you should change the parameter type to Piece* to make them consistent.
Then
Rook bRookL;
board[0][0].setPieceObj(&bRookL);
Pointer (&bRookL) can't be cast to reference (Piece&).
Most likely solution is in declaration of setPiece that should take pointer:
void setPieceObj(Piece*);
Which should actually make definition to be compilable too:
void Space::setPieceObj(Piece* p) {
pieceObj = p;
}
What's wrong in this code ? I get [Error] expected class-name before '{' token (Pralka.h line 14 )
i know there are lots of similar questions here. I've googled too but I can't get over it. So I would like to show you my code..
I wrote this very simple code to train myself on inheritance and virtual functions..
main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include "AGD.h"
using namespace std;
int main() {
Pralka p1("polar", 1250);
AGD *A;
A = &p1;
}
AGD.h:
#ifndef AGD_H
#define AGD_H
#include <iostream>
#include "Pralka.h"
class AGD {
private:
static int liczba_sprzetow;
public:
AGD(){
liczba_sprzetow++;
}
~AGD(){
liczba_sprzetow--;
}
static int get_liczba_sprzetow() {
return liczba_sprzetow;
}
virtual double get_cena() {
}
};
#endif
Pralka.h:
#ifndef PRALKA_H
#define PRALKA_H
#include <iostream>
#include <string>
using namespace std;
class Pralka : public AGD
{
private:
string marka;
double cena;
public:
Pralka(string m, double c): marka(m), cena(c){
}
Pralka(){
}
~Pralka(){
}
string get_marka() const{
return marka;
}
double get_cena() const{
return cena;
}
Pralka& operator=(const Pralka& Q){
marka=Q.marka;
cena=Q.cena;
}
};
#endif
I get also [Error] cannot convert 'Pralka*' to 'AGD*' in assignment but why? I don't understand (main.cpp line 29).
AGD.h is including Pralka.h, but it should be the other way round (Pralka.h should be including AGD.h).
The reason is that Pralka needs to see the AGD declaration to inherit from it. AGD doesn't need to see the Pralka declaration.