- 论坛徽章:
- 0
|
以下代码不使用静态方法,并合成一个文件,编译无误并可以运行,但是将程序分开后,就会报错,请各位不吝赐教,谢谢!
文件r.h
代码:
#ifndef R_H_
#define R_H_
enum logtype{operation,error,excep};
int charNum=0,xmlfilefd;
class createXML
{
private:
static char xmlbuf[];
public:
static void createXMLHead();
static int charNumber();
static void createXMLNode(enum logtype lt,char messagedescription[200]);
static void closeXMLFile();
};
#endif /* R_H_ */
文件d.h
代码:
#ifndef D_H_
#define D_H_
class createNodeMessage
{
public:
static char operationmessage[200];
static char errormessage[200];
static char exceptionmessage[200];
static void callcreateXMLNode();
};
#endif /* D_H_ */
主函数:
#include "r.h"
#include "d.h"
#include <sys/fcntl.h>
#include <sys/unistd.h>
#include <sys/file.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
struct flock* file_lock(short type, short whence)
{
static struct flock ret ;
ret.l_type = type ;
ret.l_start = 0 ;
ret.l_whence = whence ;
ret.l_len = 0 ;
ret.l_pid = getpid() ;
return &ret ;
}
void createXML::createXMLHead()
{
strcpy(createXML: mlbuf,"<?xml version=\"1.0\"?>\n<ptg-log>\n</ptg-log>" ;
xmlfilefd = open("/root/xmltest/ptg_log.xml",O_WRONLY|O_CREAT,0644);
if(xmlfilefd<0)
{
perror("open xml file failed!" ;
} else {
cout<<"xml头文件创建成功!"<<endl;
write(xmlfilefd,createXML: mlbuf,strlen(createXML: mlbuf));
close(xmlfilefd);
}
}
int createXML::charNumber()
{
//以流的方式打开文件,统计文件中字符的个数
FILE *pFile = fopen( "/root/xmltest/ptg_log.xml", "r" );
char ch;
while ( (ch = getc( pFile )) != EOF )
{
charNum++ ;
}
//计算结束
fclose( pFile );
xmlfilefd = open("/root/xmltest/ptg_log.xml",O_WRONLY|O_CREAT,0644);
return charNum;
}
void createXML::createXMLNode(enum logtype lt,char messagedescription[200])
{
cout<<"createXML begin charNum value is : "<<charNum<<endl;
//取得当前时间
time_t t = time(0);
char tmp[30];
strftime( tmp, sizeof(tmp), "%Y-%m-%d %X",localtime(&t) );
string ss=tmp;
string s;
s= ss.substr(0,16);
//开始创建xml节点信息
char node[220];
char nodeName[20];
char nodeEnd[20];
strcpy(nodeEnd,">\n</ptg-log>" ;
switch(lt)
{
case 0:
strcpy(nodeName,"operation" ;
break;
case 1:
strcpy(nodeName,"error" ;
break;
case 2:
strcpy(nodeName,"excep" ;
break;
default:
cout<<"please choose a right node name"<<endl;
break;
}
//拼接成一个xml节点
strcpy(node,"<" ;
strcat(node,nodeName);
strcat(node," time=\"" ;
strcat(node,s.c_str());
strcat(node,"\">\n" ;
strcat(node,messagedescription);
strcat(node,"\n</" ;
strcat(node,nodeName);
//strcat(node,">");
strcat(node,nodeEnd);
//给文件加上操作锁
fcntl(xmlfilefd, F_SETLKW, file_lock(F_WRLCK, SEEK_SET));
int nodelength=strlen(node);
charNum = charNum-11;
lseek(xmlfilefd,charNum,SEEK_SET);
write(xmlfilefd,node,strlen(node));
//对文件解锁操作
fcntl(xmlfilefd, F_SETLKW,file_lock(F_UNLCK,SEEK_END));
charNum = charNum+nodelength;
cout<<"node is : "<<node<<endl;
cout<<"createXMLNode charNum is : "<<charNum<<" strlen(node) length is : "<<nodelength<<endl;
}
void createXML::closeXMLFile()
{
cout<<"end this file charNum value is : "<<charNum<<endl;
close(xmlfilefd);
}
void createNodeMessage::callcreateXMLNode()
{
enum logtype log;
for(int i=0;i<3;i++)
{
switch(i%3)
{
case 0:
log=operation;
strcpy(createNodeMessage: perationmessage,"this is an operation message !");
createXML::createXMLNode(log,createNodeMessage: perationmessage);
break;
case 1:
log=error;
strcpy(createNodeMessage::errormessage,"this is an errormessage message !");
createXML::createXMLNode(log,createNodeMessage::errormessage);
break;
case 2:
log=excep;
strcpy(createNodeMessage::exceptionmessage,"this is an exceptionmessage message !");
createXML::createXMLNode(log,createNodeMessage::exceptionmessage);
break;
default:
cout<<"no input param"<<endl;
break;
}
}
}
int main()
{
createXML::createXMLHead();
createXML::charNumber();
createNodeMessage::callcreateXMLNode();
createXML::closeXMLFile();
return 0;
}
程序报编译后的错误提示:
Description Resource Path Location Type
more undefined references to `createNodeMessage::errormessage' follow main.cpp createStringXML 161 C/C++ Problem
more undefined references to `createNodeMessage::exceptionmessage' follow main.cpp createStringXML 166 C/C++ Problem
more undefined references to `createNodeMessage: perationmessage' follow main.cpp createStringXML 156 C/C++ Problem
undefined reference to `createNodeMessage::errormessage' main.cpp createStringXML 161 C/C++ Problem
undefined reference to `createNodeMessage::exceptionmessage' main.cpp createStringXML 166 C/C++ Problem
undefined reference to `createNodeMessage: perationmessage' main.cpp createStringXML 156 C/C++ Problem
undefined reference to `createXML: mlbuf' main.cpp createStringXML 38 C/C++ Problem
undefined reference to `createXML: mlbuf' main.cpp createStringXML 46 C/C++ Problem
提示说的是有些重复定义了,但是确实是没有重复定义的/ |
|