0

I am using Windows. I can't get VS Code to link the class .cpp file with the main.cpp file when I compile it.

This works:

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

using namespace std;

Burrito::Burrito(){
    b = 0;
    cout << "This is a default" << endl;
}

Burrito::Burrito(int b){
    this->b = b;
    cout << "This is parameterized " << endl;
}

int main(){
    Burrito bo;
    cout << bo.b << endl;

    Burrito* b;
    b = new Burrito(2);
    cout << b->b << endl;

    return 0;
}

This does not work:

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

using namespace std;


int main(){
    Burrito bo;
    cout << bo.b << endl;

    Burrito* b;
    b = new Burrito(2);
    cout << b->b << endl;

    return 0;
}

I've found similar problems that say to go into the User settings.json and make this change:

"code-runner.executorMap": {
        "cpp": "cd $dir && g++ *.cpp -o $fileNameWithoutExt && $fileNameWithoutExt.exe"
    }

I'm still getting this error:

C:\Users\joe\OneDrive\Documents\collegepractice\programs\bp/main.cpp:12: undefined reference to `Burrito::Burrito(int)'

I also made this change and got the same issue.

I'm a sysadmin who is going back to school next semester trying to get practice with C++ so I apologize if this isn't enough good info. If you need more I am happy to provide it.

burrito.cpp:

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

using namespace std;

Burrito::Burrito(){
    b = 0;
    cout << "This is a default" << endl;
}

Burrito::Burrito(int b){
    this->b = b;
    cout << "This is parameterized " << endl;
}

burrito.h:

#ifndef BURRITO_H
#define BURRITO_H


class Burrito
{
    public:
        int b;
        Burrito();
        Burrito(int b);
};

#endif
  • What is in `class.cpp` and in `Burrito.h`? – Gerhardh Jan 09 '22 at 00:40
  • I edited the original post and added it – someplaceinthemall Jan 09 '22 at 01:03
  • That's what I originally put but VS code suggested changing it to that. – someplaceinthemall Jan 09 '22 at 06:27
  • I moved this over to my Linux machine and I ran: "g++ *.cpp -o output" on the command line and it worked just fine. However, I also got the same error when I tried to compile it with VS Code on my Linux machine. I know I need to edit something in the settings JSON file so it links them but I can't figure out what it is. – someplaceinthemall Jan 09 '22 at 06:41
  • Don't use code runner is the basic answer, use a proper build system – Alan Birtles Jan 09 '22 at 06:41
  • this->b is okay, for both constructor functions did you try linking after removing the class name as scope? In burrito.cpp, just have Burrito (), and Burrito (int b). – Anand Sowmithiran Jan 09 '22 at 06:48
  • Did you follow [this](https://code.visualstudio.com/docs/cpp/config-linux#_build-helloworldcpp) to create tasks.json ? – Anand Sowmithiran Jan 09 '22 at 06:53
  • @AnandSowmithiran I actually followed that exact tutorial. My tasks.json is exactly the same. I tried removing the class names but that didn't work either. – someplaceinthemall Jan 09 '22 at 07:00
  • Changing the args in the tasks.json file to "args": ["-g", "burrito.cpp", "main.cpp", "-o", "${fileDirname}/${fileBasenameNoExtension}"] worked and I was also able to use the debugger too. I guess I just need to manually enter the arguments into the JSON file. That is kind of annoying but I guess this works. – someplaceinthemall Jan 09 '22 at 07:18

0 Answers0