C ++: error LNK2019: unresolved external character referenced by _main function

I have C ++ code.

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 get 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;, then OK. What can I do?

+4
source share
6 answers

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

Bird b()is a function (called bwithout parameters and returning Bird) that you did not implement.

+4
source
Bird b();

This means that b- this is a function that takes no parameters and returns a Bird. Then you try to infer the value of this function, but the function does not exist. Perhaps you want to:

Bird b;

, b Bird, .

+1

++ " Vexing Parse."

++ , b, Bird, , - Bird. , b, Bird. :

error LNK2019: unresolved external symbol "class Bird __cdecl b(void)" 

Vexing Parse - ++. , .

, , StackOverflow, C, ++.

, Bird , . , Wikipedia, , .

++ 11, brace-initializer, :

Bird b{};             // Use this if default constructed
Bird b{ ..args.. };   // Use this if you need to invoke a particular constructor

Vexing Parse - . Google it , , .

+1

Bird b();

b, Bird . :

std::cout << b;

.

Bird b;   // C++03 and C++11
Bird b{}; // C++11

, Bird::i Bird.cpp, main.cpp.

+1

:

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

'()'

+1

, , . .

0

All Articles