1 个无法解析的外部命令

程序如下:
#include
#include
using namespace std;
class Time
{
public:
string hour;
string min;
string sec;
};

int main()
{
void set_time(Time&);
void show_time(Time&);
Time t1;
set_time(t1);
show_time(t1);
Time t2;
set_time(t2);
show_time(t2);
return 0;
}

void set_time(Time&t)
{
cout<<"please enter the time:"< cin>>t.hour>>t.min>>t.sec;
}

void print_time(Time&t)
{
cout< }
发生的错误:
1>8_2.obj : error LNK2019: 无法解析的外部符号 "void __cdecl show_time(class Time &)" (?show_time@@YAXAAVTime@@@Z),该符号在函数 _main 中被引用
1>C:\Users\方辰1993\Desktop\8_2\Debug\8_2.exe : fatal error LNK1120: 1 个无法解析的外部命令
1>
求解

show_time函数只声明未定义

哪里有show_time,是不是print_time,修改为print_time看看

showtime的函数实现要一起编译或者他的cpp文件也要编译进来

编译成功,连接失败,无法解析的外部符号一般是引用了声明的函数,但是找不到函数定义,所以编译没有问题,但连接过不去,如果函数是sdk里的,检查:是否只include了头文件,没有添加lib弄?如果是自己实现的,检查:是否把存在函数定义的源码编译并一起连接了呢。

以下是我修改你的代码之后的,你看看,主要注意一下编程细节问题

#if 1
#include
using namespace std;

class Time
{
public:
string hour;
string min;
string sec;
};

int main()
{
void set_time(Time&);
void show_time(Time&);
Time t1;
set_time(t1);
show_time(t1);
Time t2;
set_time(t2);
show_time(t2);
return 0;
}

void set_time(Time&t)
{
cout << "please enter the time:" << endl;
//cin >> t.hour >> t.min >> t.sec; //此处有问题,c++库里面的string类并没有重载>>运算符,所以编译不过,修改如下
//scanf_s("%s%s%s", &t.hour, &t.min, &t.sec);
char a[100], b[100], c[100];
t.hour = gets_s(a);
t.min = gets_s(b);
t.sec = gets_s(c);
}

//void print_time(Time&t) //手误吧!
void show_time(Time&t)
{
cout << "当前时间:" << t.hour.c_str() << ":" << t.min.c_str() << ":" << t.sec.c_str() << endl; //此处调用string对象的c_str()方法,将string类型转换为char*便于cout 输出。
}
#endif