0

I am a noobie to CPP programming, i have encountered this error while trying a create a simple header, implementation and src file execution, i checked my code many times seems to be some new error and please kindly guide me through execute this in VS Code since its completely different from VS.

spacebar.hpp

#pragma once
#include <string>
//Class spacebar will hold 3 members 2 integers and 1 str, and perform some basic operations 
class spacebar{
    public:
        // constructor
        spacebar(int value1,int value2, std::string cmts){
            val1 = value1;
            val2 = value2;
            cmt = cmts;
        }
        // Getter and setter
        void set_value1(int val);
        void set_value2(int val);
        void set_cmt(std::string cmts1);
        int get_value1();
        int get_value2();
        std::string get_cmt();
        // operations
        void spacebar_op();

        // private members
    private:

        int val1,val2;
        std::string cmt;

};

spacebar.cpp

//Impelemtation file for spacebar.h
#include <iostream>
#include <string>
#include "spacebar.hpp"

void spacebar::set_value1(int val){
    val1 = val;
}
void spacebar::set_value2(int val){
    val2 = val;
}
void spacebar::set_cmt(std::string cmts1){
    cmt = cmts1;
}

int spacebar::get_value1(){
    return val1;
}

int spacebar::get_value2(){
    return val2;
}
std::string spacebar::get_cmt(){
    return cmt;
}

void spacebar::spacebar_op(){
    int temp;
    temp = val1;
    val1 = (val1-val2);
    val2 = (temp-val1);
    //cmt = cmt+" Done";

}

spacebardemo.cpp

#include <iostream>
#include "spacebar.hpp"
#include <string>

int main(){
    int val1,val2;
    std::string cmt;

    std::cout<<"In Main.cpp file ->>"<<std::endl;
    std::cout<<"This is spacebar encoder \n please enter the 3 set of values to print the key\n";
    std::cin>>val1>>val2>>cmt;


    spacebar obj(val1,val2,cmt);
    std::cout<<"\n Values \n val1 "<<obj.get_value1()<<"\n val2 "<<obj.get_value2()<<"\n cmt "<<obj.get_cmt();
    obj.spacebar_op();
    std::cout<<"\nPrivate key :";
    std::cout<<std::to_string(obj.get_value1())<<std::to_string(obj.get_value2())<<obj.get_cmt()<<std::endl;
return 0;
}

please let me know if any further info needed for better debugging. thank you in Advance!

tasks.json added

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}
  • error: `undefined reference to `spacebar::get_cmt[abi:cxx11]()' collect2.exe: error: ld returned 1 exit status ` – Tarun vignesh Selvabalaji Apr 15 '22 at 19:05
  • 1
    How do you build you application? Do you remember to build with all source files? What does you `tasks.json` file look like? – Some programmer dude Apr 15 '22 at 19:08
  • @Someprogrammerdude Hi , i have added tasks.json please have a look – Tarun vignesh Selvabalaji Apr 15 '22 at 19:13
  • In the `tasks.json` file you have the argument `"${file}"` passed to the compiler. This will only use the (single) current file being viewed by VSCode. If you want to build with multiple source files you have to list all the source files. Or better yet learn about build-systems like `make`, or project systems like CMake or Meson. – Some programmer dude Apr 15 '22 at 19:22

0 Answers0