I have error in line 7 that missing ; before * I want to make Priority Queue of Objects which gets priority by Student ID and also it is not necessary to have object pointer it can be object itself
#include<iostream>
#include<string>
using namespace std;
struct node
{
int priority;
Student * S;
node * next;
};
class Student
{
int ID;
string name;
public:
Student()
{
cin>>ID;
cin>>name;
}
void out()
{
cout<<"ID is : "<<ID<<" "<<"Name is : "<<name<<endl;
}
};
class Priority_Queue
{
node * head;
//node * back;
public:
Priority_Queue()
{
head=NULL;
//back=NULL;
}
void push(Student * Q, int a)
{
node * p=new node;
p->next=NULL;
p->priority=a;
p->S=Q;
if(head==NULL)
head=p;
else
{
node * q=head;
node * r=NULL;
while(a<=q->priority)
{
r=q;
q=q->next;
}
r->next=p;
p->next=q;
}
}
Student * pop()
{
if(isempty())
{
cout<<"Empty"<<endl;
exit(1);
}
else
{
return head->S;
head =head->next;
}
}
bool isempty()
{
if(head==NULL)
return true;
else return false;
}
};
int main()
{
Student S1,S2,S3,S4;
return 0;
}
Errors in my Code
1>d:\codes\priority queue\priority queue\1.cpp(7): error C2143: syntax error : missing ';' before '*'
1>d:\codes\priority queue\priority queue\1.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\codes\priority queue\priority queue\1.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\codes\priority queue\priority queue\1.cpp(41): error C2039: 'S' : is not a member of 'node'
1> d:\codes\priority queue\priority queue\1.cpp(5) : see declaration of 'node'
1>d:\codes\priority queue\priority queue\1.cpp(66): error C2039: 'S' : is not a member of 'node'
1> d:\codes\priority queue\priority queue\1.cpp(5) : see declaration of 'node'
Actually the problem is, that the struct node does not know about the class student, as it is defined after. A workaround is to declare Student before node, but you could aswell put Student in an extra header and include that header in the node header (personally I'd prefer that way.).
class Student;
struct node
{
int priority;
Student * S;
node * next;
};
You should use forward declaration:
struct node;
class Student;
struct node
{
int priority;
Student * S;
node * next;
};
// . . .
Related
I have been using C# for about a year now, I am attempting to move on to C++ in visual studio. Anyway I am trying to make a generic binary tree in C++ and have run into a few compile errors which I cannot seem to fix.
Initial research seemed to point towards putting the class template inside the header file, yet this gave me a host of other errors.
Some advice from someone with a bit more experience would be very much appreciated.
Thanks
Here is the code so far.
#include "stdafx.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
TreeNode<int> IntTree(1, TreeNode<int>(1), TreeNode<int>(2));
cout << IntTree.toString() << endl;
return 0;
}
template<class TData> class TreeNode
{
private:
TData Data;
TreeNode<TData>& Left;
TreeNode<TData>& Right;
void setData(TData data)
{
Data = data;
}
public:
TreeNode<TData>(TData data)
{
setData(data);
}
TreeNode<TData>(TData data, TreeNode<TData> leftNode, TreeNode<TData> rightNode)
{
setData(data);
setLeft(leftNode);
setRight(rightNode);
}
void setLeft(TreeNode<TData>& leftNode)
{
Left = leftNode;
}
void setRight(TreeNode<TData>& rightNode)
{
Right = rightNode;
}
TreeNode<TData>& getLeft()
{
return Left;
}
TreeNode<TData>& getRight()
{
return Right;
}
TData& getData()
{
return &Data;
}
string toString()
{
return Left->toString() + Data + Right->toString();
}
};
error C2228: left of '.toString' must have class/struct/union.
error C2065: 'TreeNode' : undeclared identifier.
error C2065: 'IntTree' : undeclared identifier
error C2062: type 'int' unexpected.
At the point of usage in your _tmain() TreeNode<> isn't yet declared.
You must put the complete template class declaration/definition before _tmain(), or even better put it in a separate header file, and include that.
I am getting flustrated with two errors and I have absolutely no idea what is wrong.
#ifndef ListElements
#define ListElements
#include "RentalObjects.h"
struct RentalList{
ObjectBase* content;
RentalList* Next;
};
#endif
All the time I get this error:
Error 1 error C2143: syntax error : missing ';' before '*'
Error 2 error C4430: missing type specifier - int assumed.
The RentalObjects.h file features a declaration of the ObjectBase class, which looks as follows:
class ObjectBase{
protected:
char Make[16];
char Model[16];
int Year;
float PricePerDay;
Booking* Availability;
public:
void SetMake(char* value);
void SetModel(char* value);
void SetYear(int value);
void SetPrice(float value);
bool DisposeBookings();
bool Book(int Start,int End);
char* GetMake();
char* GetModel();
int GetYear();
float GetPrice();
~ObjectBase();
};
I'd be grateful for a tip.
When declaring pointers or references, you don't need the whole class/struct definition.
Instead of:
#include "RentalObjects.h"
struct RentalList {
ObjectBase* content;
RentalList* Next;
};
you could do:
class ObjectBase;
struct RentalList {
ObjectBase* content;
RentalList* Next;
};
this could get you out of a circular include, which might be what's causing your broblem
Node.h:
#include <memory>
using namespace std;
template <typename T>
class Node
{
private:
T m_Data;
shared_ptr<Node<T>> pre_node,next_node;
public:
Node(T iData, Node* pre_ptr = nullptr, Node* next_ptr = nullptr)
:m_Data(iData),pre_node(make_shared<Node>(pre_ptr)),
next_node(make_shared<Node>(next_ptr))
};
main.cpp
#include "Node.h"
int main()
{
Node<int> head(1);
system("pause");
return 0;
}
I get an error when try to run the code:
error C2664: 'Node<int>::Node(const Node<int> &) throw()' : cannot convert argument 1
from 'Node<int> *' to 'int'
Can someone explain the problem and the way to correct it?
The problem is most likely the call to std::make_shared:
make_shared<Node>(next_ptr)
Here, the argument should be a Node or something that can be used to construct one (for instance, a T or specifically in your case, an int.) You are passing a Node*.
Don't pass a Node*. Pass an int or a Node. Or change your constructor to something like this:
Node(T iData, shared_ptr<Node> pre_ptr = nullptr, shared_pre<Node> next_ptr = nullptr)
: m_Data(iData),
pre_node(pre_ptr),
next_node(next_ptr)
I am pretty new to the concept of templates. Am I missing something?
#ifndef STACK_H
#define STACK_H
template < class T>
class Stack{
private:
struct node {
T data;
node* next;
};
node * top;
//node* getNewNode(T num);
};
//template <class T>
//(node*)Stack<T> :: getNewNode(T num){
// node * temp = new node;
// temp->data = num;
// temp->next = NULL;
// return temp;
// }
#endif
When I uncomment the function getNewNode and commend the corresponding statement to getNewNode function, complier gives error like
Why is my function getNewNode not working as expected. Where did I go wrong?
Error 7 error C2470: 'Stack<T>::getNewNode' : looks like a function
definition, but there is no parameter list; skipping apparent
body c:\users\nitinjose\documents\visual studio
2010\projects\templatequeue\templatequeue\stack.h 26 1 TemplateQueue
Error 2 error C4430: missing type specifier - int assumed. Note: C++
does not support default-int c:\users\nitinjose\documents\visual
studio
2010\projects\templatequeue\templatequeue\stack.h 26 1 TemplateQueue
Error 5 error C2146: syntax error : missing ')' before identifier
'num' c:\users\nitinjose\documents\visual studio
2010\projects\templatequeue\templatequeue\stack.h 26 1 TemplateQueue
Defining a member function outside of a class body changes the rules for what names are accessible at that point slightly. In your case the compiler has no idea what node is. You need to tell him that the node* is actually in the class Stack<T>, e.g. typename Stack<T>::node. The typename is necessary here, because node is a dependent name.
The specification of the return value is wrong. Try this
template <class T>
typename Stack<T>::node* Stack<T> :: getNewNode(T num){
// ...
}
I have this in furniture.h:
#include <iostream>
#include <string>
using namespace std;
class Furniture {
public:
Furniture();
virtual ~Furniture();
void setname(string name);
void setprice(double price);
int getprice();
string getname();
private:
string name;
int price;
protected:
static int NumberOfItems;
int Id;
}
and this in furniture.cpp
#include "furniture.h"
void Furniture::setname(string name) {
this->name = name;
}
string Furniture::getname()
{
return this->name;
}
void Furniture::setprice(double price) {
this->price = price;
}
int Furniture::getprice() {
return this->price;
}
int main() {
Furniture *model = new Furniture();
model->setname("FinalDestiny");
model->setprice(149.99);
cout<<"Model name: "<<model->getname()<<" - price = "<<model->getprice();
}
But I get some errors like:
Error 1 error C2628: 'Furniture' followed by 'void' is illegal (did you forget a ';'?) c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 2 error C2556: 'Furniture Furniture::setname(std::string)' : overloaded function differs only by return type from 'void Furniture::setname(std::string)' c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 3 error C2371: 'Furniture::setname' : redefinition; different basic types c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 5 error C2264: 'Furniture::setname' : error in function definition or declaration; function not called c:\final\facultate\poo\laborator 1\furniture.cpp 19 1 POO_lab
What am I doing wrong?
You are missing a ; at the end of the class definition in your header file.
// ...snipped...
protected:
static int NumberOfItems;
int Id;
}; // <-- here
You've forgotten a semicolon at the end of your class definition.
// ...
protected:
static int NumberOfItems;
int Id;
}; // <--
I hate that about C++ :)
Two things;
You're not ending your class definition with a ;, you need one at the end of furniture.h.
You've declared that there's a constructor and destructor, but neither is implemented in your .cpp file.