compiler suggests two candidates for class - c++

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.

Related

Error expected constructor, destructor, or type conversion before '.' token

I'm getting 3 errors when compiling my code against my college professors main.cpp
7 0 In file included from main.cpp
3 8 [Error] expected constructor, destructor, or type conversion before '.' token
28 C:\Users\Joe\Desktop\school\Makefile.win recipe for target 'main.o' failed
Here is my ntree.h file
* Ntree.h - header and implementation file for classes Tnode, Ntree //this line is the one getting the error
* Limitations: values cannot contain '(' and ')' characters.
* Can be changed by redefining TOKEN_BEGIN and TOKEN_END
#ifndef _NTREE_H
#define _NTREE_H
#define TOKEN_BEGIN '('
#define TOKEN_END ')'
#include <iostream>
#include <fstream>
#include <cstddef>
#include <vector>
#include <string>
#include <stack>
#include <sstream>
template <typename T>
class Tnode
{
private:
T value;
std::vector<Tnode*> children;
public:
Tnode (T data = {}); // copy constructor
~Tnode(); // destructor
T getValue() const; // gets the value
size_t getChildrenCount() const;
Tnode* getChild(size_t n) const; // get child by number
void setValue(T data); // sets for value
void addChild(Tnode *node); // add child node
void addChild(T data); // creates a node and adds child with a value
bool operator== ( Tnode<T> & other) const; // overload operator
};
template <typename T>
class Ntree{
private:
Tnode<T>* rootPtr;
int numOfNodes;
// utility function get node with current value
Tnode<T>* getNodeByValue(Tnode<T> *node, T data);
// utility function get value from the stream
std::string getToken(std::ifstream & ifs);
void serialize(std::ofstream & ofs, Tnode<T> *node);
public:
Ntree(); // default constructor
Ntree(T val); // constructor with root initialization
~Ntree(); // destructor
void addChildren(T parent, std::initializer_list<T> ini_list);
void serialize(std::string fname);
void deserialize(std::string fname);
bool operator== (Ntree<T> & other);
};
Try Commenting the first 3 lines. Are you sure you know what the first few lines mean?

Problems accessing private data members c++

I have three files: Stack.cc, Stack.h and stacktest.cc . I am not sure about which files to include where, and i am getting different errors because of it. Currently, the code from Stack.h is:
#ifndef STACK_H
#define STACK_H
#include<iostream>
using namespace std;
template<typename T>
class Stack
{
public:
Stack();
void push(int);
void pop();
int top();
int size();
bool empty();
private:
class Element
{
public:
int data;
Element *next;
Element(Element *n, T d) : next{n}, data{d} {}
};
Element *first;
int num;
};
#endif
#include"Stack.cc"
the (relevant, i think) code from Stack.cc is:
#include<iostream>
using namespace std;
template<typename T>
Stack<T>::Stack()
{
first=nullptr;
}
template<typename T>
void Stack<T>::push(int)
{
num++;
first = new Element(first, data);
}
Stacktest is currently just a test file attempting to call the default constructor. The errors i currently get are:
In file included from Stack.h:30:0,
from stacktest.cc:2:
Stack.cc: In member function ‘void Stack<T>::push(int)’:
Stack.cc:22:28: error: ‘data’ was not declared in this scope
first = new Element(first, data);
^
Stack.cc: In function ‘int size()’:
Stack.cc:62:11: error: ‘num’ was not declared in this scope
return num;
For some reason it wont let me access private data members. Before i didnt have the include in the .h file and instead included the .h in Stack.cc, and that worked, although wouldnt let me access the stack class from Stacktest.cc(Stacktest.cc just includes Stack.h)
Any help would be greatly appreciated, thanks.

error C2664 :cannot convert argument from 'Node<int>* ' to 'int'

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)

Class Templates - Undefined reference to my Node class' deconstructor.

I am implementing an Ordered List data structure in C++ using class templates.For simplicity, I implemented each constructor and function inline. I made my own Node class for this project.
The compiler error is pasted at the bottom of this question. "undefined reference to `Node::~Node()'". This is my first time working with templates and I've never seen this error before. I have no idea where to begin.
Any help would be appreciated!
Node.h
#ifndef NODE_H
#define NODE_H
#include <iostream>
#include <cstdlib>
template <class E>
class Node {
public:
Node(const E init_data = NULL, Node<E>* init_link = NULL){data = init_data; link = init_link;}
Node(const Node<E>& orig){data = orig.getData(); setLink = NULL;}
virtual ~Node();
E getData() const{return data;}
void setData(E newData){data = newData;}
Node<E>* getLink(){return link;}
void setLink(Node<E>* nextLink) {link = nextLink;}
private:
E data;
Node<E>* link;
};
#endif /* NODE_H */
MyOrderedList.h
#ifndef MYORDEREDLIST_H
#define MYORDEREDLIST_H
#include <iostream>
#include <cstdlib>
#include "Node.h"
template <class E>
class MyOrderedList;
template <class E>
std::ostream& operator <<(std::ostream& out, const MyOrderedList<E>& list);
template <class E>
class MyOrderedList {
public:
MyOrderedList()
{/*IMPLEMENTATION*/}
MyOrderedList(const MyOrderedList<E>& orig)
{/*IMPLEMENTATION*/}
void operator =(const MyOrderedList<E>& orig)
{/*IMPLEMENTATION*/}
virtual ~MyOrderedList()
{/*IMPLEMENTATION*/}
bool remove(E data)
{/*IMPLEMENTATION*/}
MyOrderedList<E> kLargest(int k) const
{/*IMPLEMENTATION*/}
E get(int pos) const
{/*IMPLEMENTATION*/}
void insert(E data)
{/*IMPLEMENTATION*/}
MyOrderedList<E> operator +(const MyOrderedList<E>& list)
{/*IMPLEMENTATION*/}
friend std::ostream& operator <<(std::ostream& out, const MyOrderedList<E>& list)
{/*IMPLEMENTATION*/}
private:
Node<E>* head;
int size;
};
#endif //MYORDEREDLIST_H
main.cpp
#include <cstdlib>
#include <iostream>
#include "MyOrderedList.h"
using namespace std;
int main(int argc, char** argv)
{
MyOrderedList<int> list;
list.insert(5);
std::cout << list << std::endl;;
return 0;
}
Compiler Error
g++ -o dist/Debug/Cygwin-Windows/project7_windows build/Debug/Cygwin-Windows/main.o
build/Debug/Cygwin-Windows/main.o: In function `_ZN4NodeIiE7getLinkEv':
/cygdrive/c/Users/John/Desktop/Dropbox/Data Structures/Project7 Windows/MyOrderedList.h:(.rdata$_ZTV4NodeIiE[vtable for Node<int>]+0x8): undefined reference to `Node<int>::~Node()'
/cygdrive/c/Users/John/Desktop/Dropbox/Data Structures/Project7 Windows/MyOrderedList.h:(.rdata$_ZTV4NodeIiE[vtable for Node<int>]+0xc): undefined reference to `Node<int>::~Node()'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/Cygwin-Windows/project7_windows.exe] Error 1
make[1]: * [.build-conf] Error 2
make: * [.build-impl] Error 2
As your compiler accurately states, you declared ~Node() but never defined it. You need to provide an implementation of Node::~Node().
This
class Node {
virtual ~Node();
};
declares (but does not define) a virtual destructor for Node (the templateness does not matter here). You will either need to
delete this line (thus going with the compiler provided default implementation) or
provide an definition as well:
.
class Node {
virtual ~Node() { /* put your dtor logic here */ }
};
Do not be affraid to delete the declaration uless you are planning to inherit from Node as you cannot put much logic in Node's dtor: it does not know much about its template type. Unless you are planning to delete the pointer to the next node (Node::link), which is probably a dangerous proposition

Error: expected unqualified-id before ‘<’ token

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.