注册 登录
编程论坛 C++教室

都来找错吧!我也不知道哪里错了,就是显示时间的,也不行。

黄博森 发布于 2014-12-10 15:44, 694 次点击
//time.h头文件
#ifndef TIME_H
#define TIME_H
class time
{
 private :
    int hour;
    int min;
    int sec;
 public:
     void setH(int h);
     void setM(int m);
     void setS(int s);
     int getH();
     int getM();
     int getS();
     void showtime();
};
#endif;
//实现函数showtime.cpp
#include"time.h"
#include<iostream>
using namespace std;
void time::setH(int h)
{
  hour = h;
}
void time::setM(int m)
{
  min = m;
}
void time::setS(int s)
{
  sec = s;
}
int time::getH()
{
 return hour;
}
int time::getM()
{
 return min;
}
int time::getS()
{
 return sec;
}
void time::showtime()
{
 cout<<"现在时间是:"<<hour<<"时"<<min<<"分"<<sec<<"秒"<<endl;
}
//showtime1.cpp
#include"time.h"
void main()
{
  time t;
  t.setH(12);
  t.setM(23);
  t.setS(56);
  t.showtime();
}
10 回复
#2
黄博森2014-12-10 15:46
--------------------Configuration: showtime1 - Win32 Debug--------------------
Linking...
showtime1.obj : error LNK2001: unresolved external symbol "public: void __thiscall time::showtime(void)" (?showtime@time@@QAEXXZ)
showtime1.obj : error LNK2001: unresolved external symbol "public: void __thiscall time::setS(int)" (?setS@time@@QAEXH@Z)
showtime1.obj : error LNK2001: unresolved external symbol "public: void __thiscall time::setM(int)" (?setM@time@@QAEXH@Z)
showtime1.obj : error LNK2001: unresolved external symbol "public: void __thiscall time::setH(int)" (?setH@time@@QAEXH@Z)
Debug/showtime1.exe : fatal error LNK1120: 4 unresolved externals
执行 link.exe 时出错.

showtime1.exe - 1 error(s), 0 warning(s)
#3
wp2319572014-12-10 15:46
以下是引用黄博森在2014-12-10 15:44:42的发言:

都来找错吧!我也不知道哪里错了,就是显示时间的,也不行。


这个 也不行  属实是看不懂
#4
wp2319572014-12-10 15:47
time.h 是系统文件  要不然你换个名字试试
#5
黄博森2014-12-10 15:51
回复 4楼 wp231957
不行啊,换了也不行
#6
wp2319572014-12-10 15:56
这样吧

程序代码:

#include <stdio.h>
#include "time.h"
#include "showtime.cpp"

int main(void)
{
    time t;
    t.setH(12);
    t.setM(23);
    t.setS(56);
    t.showtime();
    return 0 ;
}


因为你的showtime.cpp 并没有被编译成库文件  即便你把他弄成库文件了
因为不是系统内定的库文件  所以还需要额外附加到工程中
#7
黄博森2014-12-10 16:03
回复 6楼 wp231957
Compiling...
showtime1.cpp
e:\huangbosen2\showtime\showtime.cpp(4) : error C2653: 'time1' : is not a class or namespace name
e:\huangbosen2\showtime\showtime.cpp(6) : error C2065: 'hour' : undeclared identifier
e:\huangbosen2\showtime\showtime.cpp(8) : error C2653: 'time1' : is not a class or namespace name
e:\huangbosen2\showtime\showtime.cpp(10) : error C2065: 'min' : undeclared identifier
e:\huangbosen2\showtime\showtime.cpp(12) : error C2653: 'time1' : is not a class or namespace name
e:\huangbosen2\showtime\showtime.cpp(14) : error C2065: 'sec' : undeclared identifier
e:\huangbosen2\showtime\showtime.cpp(16) : error C2653: 'time1' : is not a class or namespace name
e:\huangbosen2\showtime\showtime.cpp(20) : error C2653: 'time1' : is not a class or namespace name
e:\huangbosen2\showtime\showtime.cpp(24) : error C2653: 'time1' : is not a class or namespace name
e:\huangbosen2\showtime\showtime.cpp(28) : error C2653: 'time1' : is not a class or namespace name
执行 cl.exe 时出错.
#8
wp2319572014-12-10 16:06
以下是引用黄博森在2014-12-10 16:03:47的发言:

Compiling...
showtime1.cpp
e:\huangbosen2\showtime\showtime.cpp(4) : error C2653: 'time1' : is not a class or namespace name
e:\huangbosen2\showtime\showtime.cpp(6) : error C2065: 'hour' : undeclared identifier
e:\huangbosen2\showtime\showtime.cpp(8) : error C2653: 'time1' : is not a class or namespace name
e:\huangbosen2\showtime\showtime.cpp(10) : error C2065: 'min' : undeclared identifier
e:\huangbosen2\showtime\showtime.cpp(12) : error C2653: 'time1' : is not a class or namespace name
e:\huangbosen2\showtime\showtime.cpp(14) : error C2065: 'sec' : undeclared identifier
e:\huangbosen2\showtime\showtime.cpp(16) : error C2653: 'time1' : is not a class or namespace name
e:\huangbosen2\showtime\showtime.cpp(20) : error C2653: 'time1' : is not a class or namespace name
e:\huangbosen2\showtime\showtime.cpp(24) : error C2653: 'time1' : is not a class or namespace name
e:\huangbosen2\showtime\showtime.cpp(28) : error C2653: 'time1' : is not a class or namespace name
执行 cl.exe 时出错.


我的是showtime.cpp 不是showtime1.cpp   要灵活一些 不要照搬
#9
wp2319572014-12-10 16:06
我的代码都是copy自你的代码  我的都能测试成功 你的为什么会出错呢  自己查一下
#10
黄博森2014-12-10 16:18
回复 9楼 wp231957
谢谢版主,成功解决,鲜花送上!
#11
新手求大神42014-12-10 20:25
回复 9楼 wp231957
我想问问你哦,我做一个工资管理系统的程序,编译和组建都没问题,就是最后的窗口显示出来后,需要自己输入数据,为什么会显示"文件打开失败"???谢谢你。
1