0

Im using visual studio code and it is giving me an undefined reference error when I run the main.cpp that has a #include .h file and another cpp file to define the function. These files run perfectly on the online code editor called Replit but not with visual studio code.

Beer.h file

#include <iostream>
using namespace std;
class Beer
{
private:
    string type;
    string where;
    double alcohol;
public:
    Beer();
    Beer(string,string,double);
    string gettype();
    void settype(string);
    string getwere();
    void setwere(string);
    double getalcohol();
    void setalcohol(double);
    bool equals(Beer,Beer);
    void serveAt(string );
    void printinfo();
    
};

Beer.cpp file

#include <iostream>
using namespace std;
#include "Beer.h"
Beer::Beer():type(""),where(""),alcohol(0){}

Beer::Beer(string Type,string Where,double Alcohol):type(Type),where(Where),alcohol(Alcohol){}

string Beer::gettype()
{    
    return type;
}
void Beer::settype(string Type)
{
    type=Type;
}
string Beer::getwere()
{
    return where;
}
void Beer::setwere(string Where)
{
    where=Where;
}
double Beer::getalcohol()
{
    return alcohol;
}
void Beer::setalcohol(double Alcohol)
{
    alcohol=Alcohol;
}

bool Beer::equals(const Beer b1, const Beer b2)
{
    if(b1.type == b2.type && b1.where == b2.where)
        return true;
    else 
        return false;
}
void Beer::serveAt(string type)
{
    if(type == "light")
        cout <<"0 to 5 Celsuis\n";
    else if(type == "pale")
        cout <<"3 to 7 Celsuis\n";
    else if(type == "blond")
        cout <<"4 to 7 Celsuis\n";
    else if(type == "stout" || "AMBER")
        cout <<"7 to 5 Celsuis\n";
    else
        cout<<"???";

}
void Beer::printinfo()
{
    cout <<type<<" beer from " <<where<< " with an alcohol content of "<<alcohol<< "%\n";
}

main.cpp file

#include <iostream>
using namespace std;
#include "Beer.h"



int main()
{
string type1;
string where1;
double alcohol1;
string type2;
string where2;
double alcohol2;
cout << "What type of beer are we looking at? ";
cin >> type1;
cout << "Where is it from? ";
cin >> where1;
cout <<"What is the alchohol content? ";
cin >> alcohol1;
cout << "\n\nWhat type of beer are we looking at? ";
cin >> type2;
cout << "Where is it from? ";
cin >> where2;
cout <<"What is the alchohol content? ";
cin >> alcohol2;

Beer bb1(type1,where1,alcohol1);
Beer bb2(type2,where2,alcohol2);
cout <<  "The Two beers are: \n";
bb1.printinfo();
bb2.printinfo();

cout << "Serving recommendations: \n";
bb1.serveAt(type1);
bb2.serveAt(type2);

cout<<"Are the beers equal? \n";
if (bb1.equals(bb1,bb2))
    cout<<"yes";
else 
 cout<< "no";

}

error in terminal

C:\Users\tanbe\AppData\Local\Temp\ccYZ7oLp.o:tempCodeRunnerFile.cpp:(.text+0x1ae): undefined reference to `Beer::Beer(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, double)'
C:\Users\tanbe\AppData\Local\Temp\ccYZ7oLp.o:tempCodeRunnerFile.cpp:(.text+0x233): undefined reference to `Beer::Beer(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, double)'
C:\Users\tanbe\AppData\Local\Temp\ccYZ7oLp.o:tempCodeRunnerFile.cpp:(.text+0x271): undefined reference to `Beer::printinfo()'
C:\Users\tanbe\AppData\Local\Temp\ccYZ7oLp.o:tempCodeRunnerFile.cpp:(.text+0x27e): undefined reference to `Beer::printinfo()'
C:\Users\tanbe\AppData\Local\Temp\ccYZ7oLp.o:tempCodeRunnerFile.cpp:(.text+0x2c1): undefined reference to `Beer::serveAt(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:\Users\tanbe\AppData\Local\Temp\ccYZ7oLp.o:tempCodeRunnerFile.cpp:(.text+0x300): undefined reference to `Beer::serveAt(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:\Users\tanbe\AppData\Local\Temp\ccYZ7oLp.o:tempCodeRunnerFile.cpp:(.text+0x36a): undefined reference to `Beer::equals(Beer, Beer)'
collect2.exe: error: ld returned 1 exit status

0 Answers0