I'm having a problem with the constructor signature in the header below. The compiler gives me the message:
error: expected ')' before '*' token
Can anybody tell me what I might be missing here?
#ifndef PRIORITYQUEUE_H
#define PRIORITYQUEUE_H
#include <iostream>
#include <cstdlib> //We'll need to use srand() and rand() as well as clock()
#include <ctime>
#include <vector>
#include <list>
#include "Graph.h" //header for Graph class
using namespace std;
class PriorityQueue
{
public:
PriorityQueue(Graph*):infiniteDist(9999);
void set_previous_node(int, int);
int get_node_value(int);
void set_node_value(int, int); //Change the node value of an element
void markVisited(int);
bool contains(int); //Does the queue contain a particular vertex?
void insertIntoQueue(int);
int top(); //pick an unvisited node with the shortest distance.
int queueSize();
void print();
private:
class vertexNode {
public:
int nodeNum;
int nodeValue;
int previousNode; //previous node visited with shortest distance from source
bool wasVisited;
};
vector<vertexNode> nodeValues;
const int infiniteDist; //value to represent infinite distance
int nodeQuantity;
};
#endif // PRIORITYQUEUE_H
The actual constructor is used as in:
PriorityQueue::PriorityQueue(Graph* graph):infiniteDist(9999)
{
...
}
You are trying to partially declare the constructor by using an initialiser expression, in the declaration of PriorityQueue(Graph*):infiniteDist(9999);. This is not allowed. The declaration (generally in the .h file) should just be:
PriorityQueue(Graph* graph);
The definition (generally in the .cpp file) should then be:
PriorityQueue::PriorityQueue(Graph* graph)
: infiniteDist(9999)
{
...
}
The reason is simply that the initaliser list is already part of the definition, i.e. what the method does, rather than just a declaration of the name and return type. Imagine that you'd use a different number (say 42) in the declaration and another (9999) in the definition, which one should be used? Hence it is not allowed.
change this
PriorityQueue(Graph*):infiniteDist(9999);
to
PriorityQueue(Graph*);
PriorityQueue(Graph*):infiniteDist(9999); is wrong.
Either you define your whole constructor in the header 9ie add the body) or you only have to to declare it with PriorityQueue(Graph*);
The solution 2 is the best one.
Related
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?
This question already has answers here:
Circular C++ Header Includes
(6 answers)
Closed 7 years ago.
Hi...
#ifndef Node_H
#define Node_H
#include <vector>
#include <stack>
#include <string>
#include <iostream>
#include "Edge.h"
#include "CongestionMap.h"
using namespace std;
class Node
{
public:
Node(){ visit = false;};
Node(int id);
~Node();
int getID();
void setLocation(int &row, int &col, GridCell *Gc);;
void displayList();
private:
int row;
int col;
int id;
bool visit;
int parrent;
int distance;
typedef vector< Edge > adjNodeList;
};
#endif
When i compile the project i get error as
project\node.h(43): error C2065: 'Edge' : undeclared identifier
project\project\node.h(43): error C2923: 'std::vector' : 'Edge' is not a valid template type argument for parameter '_Ty'...
please help me ...
Edge.h
#ifndef Edge_H
#define Edge_H
#pragma once
#include <vector>
#include <stack>
#include <string>
#include <iostream>
#include "Node.h"
using namespace std;
class Edge
{
public:
Edge() {};
Edge(Node *firstNode, Node *secNode, int inCost);
~Edge(void);
Node* getDstNode();
Node* getOrgNode();
int getCost();
private:
Node *orgNode;
Node *dstNode;
int cost;
};
#endif
As some commenters have noted, you have circular references. The code is parsed in the order it appears.
If we start in node.h, early on, it includes edge.h.
edge.h includes node.h, but that cleverly won't do anything because of the #ifdef protection, and the redundant #pragma once (they both achieve the same thing, so you might consider sticking to just one approach).
Ok, the first class definition we would encounter is that for Edge. Great, except that it refers to Node, and nobody knows what that is...because we're still in the code for edge.h that's been included into node.h.
Likely you have things happening the other way around and edge.h is being included first. The next thing that happens is that node.h is included, and it declares Node, which expects to know what Edge is, but nobody has seen that yet.
So you'll need to use forward declaration, that is in edge.h before you declare class Edge, add a line indicating what Node is:
class Node;
and conversely in node.h, provide a forward declaration for Edge. The second one is to cover the case where somebody includes node.h before they include edge.h.
As an example, if you had them both declared in the same file you would still need to do something like:
class Node; // forward declaration so that compiler knows that
// Node is a class when it gets to parsing Edge
class Edge {
...
private:
Node *orgNode;
};
class Node {
....
};
}
I am currently trying to create a list that will initialize 100 item objects but for some reason my global variables and arrays are not being recognized by the compiler. Any input would be greatly appreciated. Also if you see anything that I am doing wrong, criticism is highly appreciated.
Header for my List:
#ifndef List_hpp
#define List_hpp
#include <stdio.h>
#include <string>
#include <iostream>
#include "Item.hpp"
using namespace std;
class List
{
private:
static int itemcount;
Item items[100];
public :
List(){
this->itemcount = 0;
};
void addItem(string, string, int, double);
void removeItem(string);
void display();
}; //End List Class
#endif /* List_hpp */
CPP file:
#include "List.hpp"
//This method will add a new item
void addItem(string name, string unit, int quantity, double price){
Item newItem(name, unit, quantity, price);
itemcount++;
}
void removeItem(string name){
}
void display(){
}
In the CPP File of the class List you must put the class name before every function implementation like:
void List::addItem...
void List::removeItem...
void List::display..
I think the implementation of addItem is not completed yet. You didn't put the new created item in the list.
You must declare a copy constructor for the class Item or use dynamic allocation.
I'm trying to create a vector which will store objects. I have added to the header file of the class as a private data member.
I am trying to initialize this vector as being empty (so that I can add objects to it later on in the program) but when I compile this program to test, this error is returned:
...error: '_bookingVector' was not declared in this scope|
I think the problem is with my initialization list on my default constructor(_bookingVector is obviously the vector):
Schedule::Schedule() : _bookingVector()
{ }
Is my syntax wrong? Or are vectors initialized differently?
Here is my code:
Schedule.h
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include "Booking.h"
#include <vector>
using namespace std;
class Schedule
{
public:
Schedule();
void AddBooking(int bday, int btime, int btrainer, int bid);
void RemoveBooking(int bday, int btime);
void DisplaySchedule();
void DisplayAvailableTimeSlots();
//For Testing
void DisplayDebug();
private:
vector<Booking> _bookingVector;
};
#endif // SCHEDULE_H
Schedule.cpp
#include "Schedule.h"
#include "Booking.h"
#include <vector>
#include <iostream>
Schedule::Schedule() : _bookingVector()
{ }
void AddBooking(int bday, int btime, int btrainer, int bid){
Booking bookingObject(bday, btime, btrainer, bid);
_bookingVector.push_back(bookingObject);
}
void DisplayDebug(){
for(int i = 0; i < _bookingVector.size(); ++i){
cout << _bookingVecotr[i] << endl;
}
}
I'm very eager to learn what I'm doing wrong and fix it.
The issue is not with the constructor, which looks fine if unnecessary1. The issue is that you have defined AddBooking and DisplayDebug as non-member functions, but these should be members in order to access other members of the class.
Modify the definitions to be in the scope of the Schedule class thus:
void Schedule::AddBooking(int bday, int btime, int btrainer, int bid) { ...
^^^^^^^^^^
void Schedule::DisplayDebug(){ ...
^^^^^^^^^^
Also, don't say using namespace std in a header file (I'd go further and say don't say it anywhere but there isn't universal agreement on that.)
1 Your default constructor does not do anything that the compiler-generated one wouldn't do. You can safely remove it.
When I do this, my compiler complains. There are 3 errors that emerge, though no error messages visible:
#include <stdlib.h>
#include <vector>
#include <string>
#include "ParseException.h"
#include "CycleFoundException.h"
#include "UnknownTargetException.h"
using namespace std;
class Maker
{
private:
vector<Node> storage;
public:
Maker(string file) throw (ParseException, CycleFoundException, UnknownTargetException);
vector<string> makeTarget(string targetName);
};
struct Node
{
string target;
vector<string> dependencies;
string command;
int discoverytime;
int finishtime;
int visited;
Node* next;
};
The compiler does not like my vector<Node> storage declaration. When I do vector<int> storage instead, it compiles without complaint. Is it wrong to declare an object of one class in another class? I thought this was alright.
Looks like you need to put the definition of Node before the definition of Maker.
You use the type name Node in the definition of Maker (in the line vector<Node> storage), but because you haven't defined Node yet the compiler doesn't know what it is.