2013-10-02 98 views
0

首先的模板,而我试图调用的函数是不是在外部库。这是下面的部分(我正在回C++,所以我想我会写我自己的cron守护程序执行启动)C++未定义的参考和代码

这里是我的代码:

#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <time.h> 

using namespace std; 

//Global Variables: 
    //local time in the form of a tm struct 
    struct tm sysclocktime; 
    /* 
    * tm_sec int seconds after the minute 0-60* 
    * tm_min int minutes after the hour 0-59 
    * tm_hour int hours since midnight 0-23 
    * tm_mday int day of the month 1-31 
    * tm_mon int months since January 0-11 
    * tm_year int years since 1900  
    * tm_wday int days since Sunday 0-6 
    * tm_yday int days since January 1 0-365 
    * tm_isdst int Daylight Saving Time flag 0-? 
    */ 



//Function Declarations: 
    bool fexists(const char *filename); 
    char* timetostring(struct tm timeobj); 

//Functions:  
int main(int argc, char* argv[]){ 
    //error counter 
    int errors = 0; 

    //Set the current time by the system's clock 
    time_t  now = time(0); 
    sysclocktime = *localtime(&now); 
    //Print the current time to cout: 
    cout << "Current time is: " << timetostring(sysclocktime) << endl; 

    //See if mcron.cfg (mcron config file) exists 
    if(fexists("mcron.cfg")){ 
     //if mcron.cfg exists, read data from it 
     cout << "mcron.cfg existiert bereits!" << endl; 
    }else{ 
     //if mcron.cfg doesn't exist, create it 
     cout << "mcron.cfg existiert nicht! :(" << endl; 
    } 
    return errors; 
} 


/* 
* Function which tells whether a file exists 
*/ 
bool fexists(const char *filename){ 
    ifstream ifile(filename); 
    return ifile; 
} 

/* 
* Converts the pointer to a time struct into dates and times in a string 
* format like so: YYYY-MM-DD-HH-MM-SS 
* For example: 2013-07-15-16-14-36 
*/ 
char* timetostring(struct tm* timeobj){ 
//Declare local variable charstring which will be used to create the string of characters 
    char* charstring = new char[20]; 
    //Initialize charstring with relevant time data 
sprintf(charstring,"%4d-%2d-%2d-%2d-%2d-%2d",(timeobj->tm_year+1900), (timeobj->tm_mon+1),timeobj->tm_mday,timeobj->tm_hour,timeobj->tm_min,timeobj->tm_sec); 
    //Print the string of characters to the command line: 
    cout << "timetostring(): " << charstring; 
    return charstring; 
} 

这里是我所得到的当我尝试编译它时:

[[email protected] ~/code/mcron]$ g++ mcron.cpp -o mcron 
/tmp/cc9M4We8.o: In function `main': 
mcron-nostring.cpp:(.text+0xd1): undefined reference to `timetostring(tm)' 
collect2: Error: ld returned 1 as it's exit status 

我在做什么错?

+1

'字符* timetostring(结构TM timeobj);'VS'的char * timetostring(结构TM * timeobj);'注意区别? – jrok

回答

1

你的声明缺少一个指针:

char* timetostring(struct tm* timeobj); 
         //^here 

(加上你需要适应呼叫)

+0

谢谢。我不能相信我错过了这一点。 – KG6ZVP