简体   繁体   中英

How to properly separate class header and cpp in VSC?

I'm trying to create a class in VSC but im getting the "undefined reference to" error when i run the main.cpp, but when i run all cpp's i get "multiple definition" and "first defined here" error. Could someone help me out on this?

Here's a part of the first error:

c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\davim\AppData\Local\Temp\cc6yciyu.o:main.cpp:(.text+0x1e6): undefined reference to `RBTree::inorder()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\davim\AppData\Local\Temp\cc6yciyu.o:main.cpp:(.text+0x1f5): undefined reference to `RBTree::~RBTree()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\davim\AppData\Local\Temp\cc6yciyu.o:main.cpp:(.text+0x205): undefined reference to `RBTree::~RBTree()'
collect2.exe: error: ld returned 1 exit status

The second one:

c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\davim\AppData\Local\Temp\cclWqRsx.o:RBTree.cpp:(.text+0xe4): multiple definition of `RBTNode::getRight()'; C:\Users\davim\AppData\Local\Temp\ccyEVyWh.o:main.cpp:(.text+0xe4): first defined here
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\davim\AppData\Local\Temp\cclWqRsx.o:RBTree.cpp:(.text+0xf6): multiple definition of `RBTNode::setParent(RBTNode*)'; C:\Users\davim\AppData\Local\Temp\ccyEVyWh.o:main.cpp:(.text+0xf6): first defined here
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\davim\AppData\Local\Temp\cclWqRsx.o:RBTree.cpp:(.text+0x10e): multiple definition of `RBTNode::getParent()'; C:\Users\davim\AppData\Local\Temp\ccyEVyWh.o:main.cpp:(.text+0x10e): first defined here
collect2.exe: error: ld returned 1 exit status

Header:

#ifndef RBTREE_H
#define RBTREE_H
#include "RBTNode.h"
#include <bits/stdc++.h>

class RBTree
{
private:
    RBTNode* root;
    void rotateLeft(RBTNode *&, RBTNode *&);
    void rotateRight(RBTNode *&, RBTNode *&);
    void fixViolation(RBTNode *&, RBTNode *&);
    RBTNode* aux_insert(RBTNode* root, RBTNode* p);
    void inorderHelper(RBTNode* root);
public:
    RBTree(/* args */);
    ~RBTree();
    void inorder();
    void insert(int data);
};

#endif

cpp:

#include "RBTree.h"

RBTree::RBTree(/* args */)
{
    this->root = NULL;
}

RBTree::~RBTree()
{
}

void RBTree::rotateLeft(RBTNode *&root, RBTNode *&p)
{
   //implementation
}

main:

#include <iostream>
#include "RBTree.h"

using namespace std;

int main()
{
    RBTree tree; 

    tree.insert(7); 
    tree.insert(6); 
    tree.insert(5); 
    tree.insert(4); 
    tree.insert(3); 
    tree.insert(2); 
    tree.insert(1); 

    cout << "Inoder Traversal of Created Tree\n"; 
    tree.inorder(); 

    return 0; 
}

The hardest thing of all is to find a black cat in a dark room, especially if there is no cat.

It appears that your issue is in the "RBTNode.h" file that you didn't show.

It is included in both main.cpp and RBTree.cpp .

I suspect it has some definitions in it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM