0

I'm trying to write a List class, but have a problem:

E:\junior_req\test_project\Node.cpp|6|multiple definition of `Node::print()'|

My code:

Node.h:

class Node {
    Node* next;
    int value;
public:
    Node() {next = nullptr; value = 0;}
    Node(int t) {this->value = t; next = nullptr;}
    Node(const Node& t) {this->next = t.next; value = t.value;}
    ~Node() {}

    int getValue() {return value;}
    Node* getNext() {return next;}

    void setValue(int t) {value = t;}
    void setNext(Node* t) {next = t;}

    Node& operator=(Node t);

    void print();
};

Node.cpp

#include "Node.h"
#include <iostream>
using namespace std;
void Node::print() {
    cout << this->getValue() << endl;
    if(this->getNext() != nullptr) this->getNext()->print();
}

List.h and List.cpp classes are same, but there are no errors.

Main.cpp:

#include <iostream>
#include "List.h"
#ifndef NODE_H
#define NODE_H
using namespace std;
int main()
{
    cout << "Hello world!" << endl;
    List l;
    l.add(1);
    Node* one;
    l.add(one);
    one->print();
    return 0;
}
#endif // NODE_H

#pragma once doesn't help

What is wrong with my code?

hellow
  • 12,430
  • 7
  • 56
  • 79
Kseniia
  • 1
  • 1

0 Answers0