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)
Related
I am learning c++98 and I get this error while compiling my Node class:
Node.cpp:13:1: error: no declaration matches ‘Node::Node(Node::dataType&)’
13 | Node::Node(dataType& initData){
| ^~~~
In file included from Node.cpp:7:
node.h:13:7: note: candidates are: ‘Node::Node(const Node&)’
13 | class Node{
| ^~~~
node.h:19:5: note: ‘Node::Node(const dataType&)’
19 | Node(const dataType&);
| ^~~~
node.h:13:7: note: ‘class Node’ defined here
13 | class Node{
| ^~~~
This is my Node.cpp
#include "node.h"
#include <cstdlib>
using namespace std;
Node::Node(dataType& initData){
data = initData;
next = NULL;
prev = NULL;
}
void Node::setData(dataType& newData){
data = newData;
}
void Node::setNext(Node* nextLink){
next = nextLink;
}
void Node::setPrev(Node* prevLink){
prev = prevLink;
}
dataType Node::getData(){
return data;
}
Node* Node::getPrev(){
return prev;
}
Node* Node::getNext(){
return next;
}
This is my Node.h
#ifndef TYLER_NODE
#define TYLER_NODE
#include <iostream>
#include <cstdlib>
#include "EToll.h"
class Node{
public:
//TYPEDEF
typedef EToll dataType;
//CONSTRUCTOR
Node(const dataType&);
void setData(const dataType&);
void setNext(Node*);
void setPrev(Node*);
dataType getData() const;
const Node* getPrev() const;
Node* getPrev();
const Node* getNext() const;
Node* getNext();
private:
dataType data;
Node* next;
Node* prev;
};
#endif
It's supposed to be a simple class that holds instances of EToll (from EToll.h) for use in a linked list but I get this error. It looks like the compiler is mistaking the Node class for the Node constructor but I'm still learning so I'm not too sure
The confusion here stems from the compiler’s thoroughness in reporting the candidates. The first is the implicitly declared copy constructor, which refers spatially to the class name because, well, it’s implicit. That’s not very helpful to see here because you can’t define an implicitly declared function anyway, but that check comes after “Did you match any constructor at all?”, which you didn’t because of the const.
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 a beginner in c++ so please excuse me if I my mistakes below turn out to be silly. Still, I am stuck with my code and would appreciate any help.
I get the following error when trying to compile through make via g++:
In file included from A.cpp:2:
List.h:20: error: ‘List’ is not a template type
A.cpp: In member function ‘void A::NowyObiekt(int)’:
A.cpp:6: error: ‘list_a’ was not declared in this scope
make: *** [A.o] Error 1
My code is separated into the following tiny files:
A.h : http://pastebin.com/QQ04xx2j (header)
A.cpp : below
#include "A.h"
#include "List.h"
void A::NewObject(int i)
{
list_a.Add(i);
}
int A::Compare(int a, int b)
{
if ( a>b ) return 1;
if ( a<b ) return -1;
else return 0;
}
List.h : below (header)
#ifndef LIST_H
#define LIST_H
template<typename T>
class Node
{
Node()
{
nxt = pre = 0;
}
Node(const T& el, Node *n = 0, Node *p = 0 )
{
dana = el; nxt = n; pre = p;
}
T dana;
Node *nxt, *pre;
};
template<typename T>
class List
{
public:
List()
{
head = tail = 0;
}
void Add(const T&);
protected:
Node<T> *head,*tail;
};
#endif
List.cpp : http://pastebin.com/a3HQ9yZ4
prog.cpp : below (main)
#include "List.h"
#include "A.h"
int main()
{
int i = 5;
class List list_a;
class A obj;
obj.Add(i);
}
and the makefile is : http://pastebin.com/GTR5jW54
As noted, I am still a beginner, so please be understanding. I would be thankful for any help and clear explanations. Thanks in advance.
There are a couple of problems with your code: The first is that you don't declare any variable named list_a anywhere. That error should be pretty obvious. The other is that you use the List class without giving it template parameters.
And last a small note about your question: As your files are indeed very small, you could put them in the question and not link to them.
Edit: About the List template problem.
You already use Node properly in List, i.e. declare the nodes as Node<T>. When you use List you simply has to do the same. For example, to declare a list of integers:
List<int> my_int_list;
Also, as you only use public functions in List from the class A, you don't need the friend declaration. If you do need to use protected or private members (which IMO is a sign of bad design) you need to make that friend-declaration templated as well:
friend class List<sometype>;
And finally, your code will not compile anyway... The reason being that when you are using a template-class, the whole class has to be fully defined (i.e. complete with its function implementations). You can solve this by putting the functions in the header file. And when defining the functions, you need the template parameter there as well:
template<typename T>
void List<T>::Add(const T& el)
{
Node<T>* head = new Node<T>(el);
if ( Compare(el,i) > i )
std::cout << "Ok" << std::endl;
}
Note that I added the template parameter in a couple of places.
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 am trying to make some sort of templated Queue class. It seems ok but I am getting 2 errors in the same line which I can't figure out why. The errors appear in the implementation file .cpp where I am trying to give the definition for the destructor. Here is the code of the header file of the class:
#ifndef QUEUETP_H_INCLUDED
#define QUEUETP_H_INCLUDED
template <class T>
class QueueTp
{
private:
struct Node { T item; struct Node * next;};
enum {QSIZE = 10};
//Queue's head
Node *head;
//Queue's tail
Node *tail;
int size;
int maxsize;
QueueTp(const QueueTp & q);
QueueTp & operator=(const QueueTp & q) { return *this;}
public:
QueueTp(): size(0),head(0),tail(0),maxsize(QSIZE) {};
QueueTp(int q = QSIZE): size(0),head(0),tail(0),maxsize(q) {};
~QueueTp();
bool isEmpty(){return size==0;}
bool isFull() {return size==maxsize;}
int sizecur() {return size;}
bool push(const T& t);
bool pop(T& t);
};
#include "QueueTp.cpp"
#endif // QUEUETP_H_INCLUDED
And here is the definition of the destructor in the implementation file:
#include "QueueTp.h"
#include <iostream>
using namespace std;
typename <class T> //<-<-<- in this line I am getting the two errors
QueueTp<class T>::~QueueTp()
{
Node *ptr;
cout<<endl<<"Deleting the queue...";
while (head !=NULL)
{
ptr = head->next;
delete head;
head = ptr;
}
}
//......other method definitions
The errors are pointed above and the specific error messages I get from the compiler are the ones below.
error: expected nested-name-specifier before ‘<’ token|
error: expected unqualified-id before ‘<’ token|
||=== Build finished: 2 errors, 12 warnings ===||
Please use "template" instead of "typename" on the line where you are getting the two error messages! I find that most of the time, an unidentified keyword or a real keyword in the wrong place often gives errors similar to an undefined type, the next symbol after it would cause an error.