Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: error LNK2019: unresolved external symbol referenced in function _main

Tags:

c++

lnk2019

I have some code on C++.

Bird.h

class Bird
{
    std::string s;
    static int i;
public:
    Bird();

    ~Bird();
    friend std::ostream& operator<<(std::ostream& os, Bird& b);

};

Bird.cpp

#include <iostream>
#include <sstream> 
#include "Bird.h"



Bird::Bird()
{
    ++i;
    std::stringstream ss;
    ss<<"Bird#";
    ss<<i;
    s = ss.str();
}
Bird::~Bird()
{
    i--;
}
std::ostream& operator<<(std::ostream& os, Bird& b)
{
    const char* chr = (b.s).c_str();
    return os << chr << std::endl;
}

Main.cpp

#include <iostream>
#include <sstream> 
#include "Bird.h"


int Bird::i=0;

int main()
{
    Bird b();
    std::cout << b;
}

I am getting the following error:

Main.obj : error LNK2019: unresolved external symbol "class Bird __cdecl b(void)" (?b@@YA?AVBird@@XZ) referenced in function _main

But if I create Bird b; it's OK. What can I do?

like image 987
imladris Avatar asked Feb 15 '23 07:02

imladris


1 Answers

You meant to write Bird b; to create a Bird object.

Bird b() is a function (called b, taking no parameters and returning a Bird), which you haven't implemented.

like image 147
Bathsheba Avatar answered Apr 08 '23 11:04

Bathsheba