Heidel
Heidel

Reputation: 3254

C++ error LNK2019

I need to create string variable string time and it should look like 14:58.
I created function

string SetTime() {
long double h = (long double)(rand()%25);
long double m = (long double)(rand()%60);

string hour = to_string(h);
string minutes = (m <= 9 ? "0" : "" ) + to_string(m);

string time = hour + ":" + minutes;
return time;
}

but when I try to use it

string str = SetNumber();
cout << str;

I get
error LNK2019: link to unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl SetNumber(void)" (?SetNumber@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) in _wmain

What's wrong and how to fix it?

Upvotes: 0

Views: 100

Answers (2)

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132974

Your function is called SetTime while you're calling SetNumber. The linker cannot find the definition of SetNumber. It is interesting that you are getting a linker error rather than a compiler error. It means that you've declared SetNumber.

Upvotes: 3

suitianshi
suitianshi

Reputation: 3340

you should call SetTime, not SetNumber

Upvotes: 1

Related Questions