I’m trying to write a linked queue in C++, but I’m failing so far. I’ve created 2 files by now: my main.cpp and box.h. When trying to use my box, I receive the following message:
Description Resource Path Location Type
conversion from ‘Box*’ to
non-scalar type ‘Box’
requested main.cpp /QueueApplication line
14 C/C++ Problem
My code is as follows:
box.h
#ifndef BOX_H_
#define BOX_H_
template<class T>
class Box
{
public:
Box(T value)
{
this->value = value;
this->nextBox = NULL;
}
T getValue()
{
return this->value;
}
void setNext(Box<T> next)
{
this->nextBox = next;
}
private:
T value;
Box<T> nextBox;
};
#endif /* BOX_H_ */
main.cpp
#include<iostream>
#include "box.h"
using namespace std;
int main(int argc, char** argv)
{
Box<int> newBox = new Box<int>();
cout << "lol";
cin.get();
cin.ignore();
return 0;
}
Could you guys help me?
PS: before someone ask me why not to use stl … I’m in a data structures class.
The problem is with this line
The
newoperator returns a pointer to aBoxobject created on the heap. The pointer will be of typeBox<int>*. The left side of that expression declares aBoxobject. You can’t directly assign a pointer-to-X to an X. You should probably just omit thenewkeyword unless you have a reason to want to manage the storage lifetime of the object manually. Incidentally, I’m betting you come from Java, wherenewis always required to create objects. Not so in C++.Also I think it’s awesome that your data structures class is introducing you to templates right off the bat.