新手求救undefined reference to `common::cur_time()'错误

C、C++和Java语言
回复
csr_hema
帖子: 4
注册时间: 2010-12-24 13:52
送出感谢: 0
接收感谢: 0

新手求救undefined reference to `common::cur_time()'错误

#1

帖子 csr_hema » 2010-12-24 13:58

三个文件:
common.h

#ifndef COMMON_H
#define COMMON_H

class common
{
public:
common(){};
~common();
void cur_time(void);
void output();
//void cur_time(int);
};

#endif



common.cpp

#include <time.h>
#include <stdio.h>
#include <string.h>

#include "common.h"

using namespace std;

common::common()
{}

~common::common()
{}

/*void common::cur_time(int pParam)
{
char *wday[]={"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep); // 获取当前时间
printf("%d年%02d月%02d日",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
printf("%s %02d:%02d:%02d\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);
}*/

void common::cur_time(void)
{
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep); /* 获取当前时间 */
printf("%d-%02d-%02d ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
printf("%02d:%02d:%02d\n",p->tm_hour,p->tm_min,p->tm_sec);
}

void common::output()
{
printf("hello\n");
}


client.cpp

#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <arpa/inet.h>

#include "common.h"

using namespace std;

#define NO_FLAGS_SET 0

typedef int SOCKET;
#define SOCKET_ERROR (-1)
#define INVALID_SOCKET (-1)

#define PORT (u_short) 10001
#define DEST_IP_ADDR "192.168.255.199" //Server address

//void cur_time(void);
common *con;

int Connect(int pParam)
{
sockaddr_in destSockAddr;
SOCKET destSocket;
unsigned long destAddr;
int status;
int numsnt;
const char *toSendtxt = "Test String";
char buf[1024];
con = new common();

destSockAddr.sin_addr.s_addr = inet_addr(DEST_IP_ADDR); //.sin_addr.s_addr
destSockAddr.sin_port = htons(PORT);
/* specify the address family as Internet */
destSockAddr.sin_family = AF_INET;

/* create a socket */
destSocket = socket(AF_INET, SOCK_STREAM, 0);

if (destSocket == INVALID_SOCKET)
{
cerr << "ERROR: socket unsuccessful" << endl;

return(1);
}

cout << "Trying to connect to IP Address: " << DEST_IP_ADDR << endl;

/* connect to the server */
status = connect(destSocket, (sockaddr *)&destSockAddr, sizeof(destSockAddr));

if (status == SOCKET_ERROR)
{
cerr << "ERROR: connect unsuccessful" << endl;

status = close(destSocket);

if (status == SOCKET_ERROR)
{
cerr << "ERROR: closesocket unsuccessful" << endl;
}

return 1;
}

cout << "Connected..." << endl;

int count = 1;
while(1)
{
//cout << "FrameNo:" << count << "\tSendingContent:" << toSendtxt << "\ttime:" << time(NULL) << endl; //asctime(localtime(&rawtime))

cout << "FramNo:" << count << "\tSendingContent:" << toSendtxt << "\ttime:";

con->output();
con->cur_time();

numsnt = send(destSocket, toSendtxt, (int)strlen(toSendtxt) + 1, NO_FLAGS_SET);

if (numsnt != (int)strlen(toSendtxt) + 1)
{
cout << "Connection terminated" << endl;

status = close(destSocket);

if (status == SOCKET_ERROR)
{
cerr << "ERROR: closesocket unsuccessful" << endl;
}

return 1;
}

memset(&buf,0,sizeof(buf));

status = recv(destSocket,buf,sizeof(buf),0);

if(SOCKET_ERROR == status)
{
close(destSocket);
return -1;
}

cout << "EchoNo:" << count << "\tContent:" << buf << endl;

/*if(count % 7 == 1)
{
Beep(523,500);
}
else if(count % 7 == 2)
{
Beep(578,500);
}
else if(count % 7 == 3)
{
Beep(659,500);
}
else if(count % 7 == 4)
{
Beep(698,500);
}
else if(count % 7 == 5)
{
Beep(784,500);
}
else if(count % 7 == 6)
{
Beep(880,500);
}
else if(count % 7 == 0)
{
beep(988,500);
}*/

count++;

/* Wait before sending the message again */
sleep(1);
} /* while */
}

int main()
{
Connect(0);

return 0;
}

/*void cur_time(void)
{
char *wday[]={"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep); // 获取当前时间
printf("%d-%02d-%02d ",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
printf("%02d:%02d:%02d\n",p->tm_hour,p->tm_min,p->tm_sec);;
}*/

现在编译出现错误:
g++ -o client client.cpp
/tmp/ccyEy2E2.o: In function `Connect(int)':
client.cpp:(.text+0x21a): undefined reference to `common::output()'
client.cpp:(.text+0x227): undefined reference to `common::cur_time()'
collect2: ld returned 1 exit status

向各位请教,谢谢大家
csr_hema
帖子: 4
注册时间: 2010-12-24 13:52
送出感谢: 0
接收感谢: 0

Re: 新手求救undefined reference to `common::cur_time()'错误

#2

帖子 csr_hema » 2010-12-24 15:07

各位帮帮忙阿,这个代码不是很复杂呀,我是一个新手,关键是我不知道在ubuntu下怎么引用另外一个文件中的函数,因此造成这个错误了,请大家给个大致的方法吧,非常感谢各位了
回复

回到 “C/C++/Java”