Undefined Reference

Hi guys :wave:t4:

Can anyone please help me to solve Undefined Reference error?

There is no definition for Length::Length(int) in your Length class.

1 Like

Thank you POMPOMBALLS for replying. I think we better check our code for clarity

After analysing my code, can you please tell me where I am incurring this error?

Use

3 backticks (`) to write your code so that the problem can be reproduced easily.

Sure buddy!
Folder name is EqualityOperator

EqualityOp.hpp

#ifndef EQUALITYOPERATOR_EQUALITYOP_H
#define EQUALITYOPERATOR_EQUALITYOP_H

class Length{
    public:
        explicit Length(int value);
        bool operator==(const Length& source)const;
        bool operator!=(const Length& source)const;
        bool operator==(int value)const;
        bool operator!=(int other)const;
    private:
        int value;
};

#endif

EqualityOp.cpp

#include "EqualityOp.hpp"

Length::Length(int value): value{value} {}

bool Length::operator==(const Length& source)const{
    return value == source.value;
}

bool Length::operator!=(const Length& source)const{
    return !(operator==(source));
}

bool Length::operator==(int value)const{
    return this->value == value;
}

bool Length::operator!=(int other)const{
      return !(value == other);
}

MainEqual.cpp

#include "EqualityOp.hpp"
#include <iostream>

using namespace std;

int main(){
    Length first{10};
    Length second{10};

    cout<<((first == second)? "true":"false")<<endl;

    cout<<boolalpha<<first.operator==(second)<<endl;
    if(first != second)
        cout<<"true"<<endl;
    else {cout<<"false"<<endl;}

    cout<<((first == 10)? "true":"false")<<endl;
    cout<<((first != 20)?"yes":"no")<<endl;

    return 0;
}

Your files must be known to the linker. LNK2019 (in Visual Studio) is a linking error for unresolved external symbol(s). Make sure your file is included in the project as shown (again, Visual Studio). The code works as it should.

For more information, look here (discussion).

1 Like

I’ll check it and will get back to you buddy. I appreciate your effort!
:smiley:

I am using Visual Studio Code rather than Visual Studio. Can this problem be solved in VS Code or should I start to use Visual Studio for further practice?

It should be available in Workspace settings. If not, then there should be a setting for syncing project files.

1 Like