C ++ LINK2001错误

时间:2012-07-31 02:29:04

标签: c++ linker compiler-errors project

我非常感谢我对项目可能出错的一些建议。

我正在尝试编译我的项目,但我收到以下错误:

phoneBook.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * phoneBook::lastName" (?lastName@phoneBook@@0PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)
phoneBook.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * phoneBook::firstName" (?firstName@phoneBook@@0PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)
phoneBook.obj : error LNK2001: unresolved external symbol "private: static int * phoneBook::phone" (?phone@phoneBook@@0PAHA)
phoneBook.obj : error LNK2001: unresolved external symbol "private: static char (* phoneBook::dateOfBirth)[10]" (?dateOfBirth@phoneBook@@0PAY09DA)

这是头文件:

//phoneBook.h header file
#ifndef PHONEBOOK_H
#define PHONEBOOK_H
#include <string>
using namespace std;

class phoneBook
{
public:
 phoneBook();                                   //default constructor
 phoneBook(string lastN[], string firstN[], 
            int phoneNumber[], char date[][10]);        //overloaded constructor
 void setLastName(string lastN, int idNum);     //sets last name for a given ID number,
                                                //-IDs range from 1 to 50
 void setFirstName(string firstN, int idNum);   //sets first name for a given ID number
 void setPhoneNumber(int phoneNum, int idNum);  //sets the phone number for a given ID number
 void setDate(char dateOB[], int idNum);            //sets the date of birth for a given ID number
 void loadFromFile(char fileName[]);                //loads phone book data from a local file
 int getPhoneNumber(string lastN, string firstN);//returns phone number for particular person
 void getDOB(string lastN, string firstN, char testDOB[]);  //sets date of birth for a particular person to testDOB array
 void printPhone_DOB(string lastN, string firstN);  //print phone number and date of birth
                                                    //-for a given person
 void printNamesForDOB(char month[]);           //print names of people with birthdays on given numeric month
 void saveData();                               //save data back to the local file
private:
 string filename;
 static string lastName[50];
 static string firstName[50];
 static int phone[50];
 static char dateOfBirth[50][10];
};
#endif

实施档案:

//phoneBook.cpp implementation file
#include "phoneBook.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//default constructor
phoneBook::phoneBook()
{
//declare variables
int i, j;
string lastName[50];
string firstName[50];
int phone[50];
char dateOfBirth[50][10];

//set variables to zero/null
filename = "";
for(i=0; i<50; i++)
{
    lastName[i] = "";
    firstName[i] = "";
    phone[i] = 0;

    //set date to zero, in format 00/00/0000
    for(j=0; j<10; j++)
    {
        dateOfBirth[i][j] = '0';
        if(j==2 || j==5)
            dateOfBirth[i][j] = '/';
    }
}
}

//overloaded constructor
phoneBook::phoneBook(std::string lastN[], std::string firstN[], int phoneNumber[], char date[][10])
{
//declare variables
int i, j;
string lastName[50];
string firstName[50];
int phone[50];
char dateOfBirth[50][10];

//set variables to zero/null
filename = "";
for(i=0; i<50; i++)
{
    lastName[i] = lastN[i];
    firstName[i] = firstN[i];
    phone[i] = phoneNumber[i];
    for(j=0; j<10; j++)
        dateOfBirth[i][j] = date[i][j];
}
}

//setLastName function, sets the last name for a given ID number
void phoneBook::setLastName(string lastN, int idNum)
{
lastName[idNum-1] = lastN;
}

//setFirstName function, sets the first name for a given ID number
void phoneBook::setFirstName(string firstN, int idNum)
{
firstName[idNum-1] = firstN;
}

//setPhoneNumber function, sets the phone number for a given ID number
void phoneBook::setPhoneNumber(int phoneNum, int idNum)
{
phone[idNum-1] = phoneNum;
}

//setDate function, sets the date of birth for a given ID number
void phoneBook::setDate(char dateOB[], int idNum)
{
//declare variables
int i;

//set DOB
for(i=0; i<10; i++)
    dateOfBirth[idNum][i] = dateOB[i];
}

//loadFromFile function, loads phone book data from a local file
void phoneBook::loadFromFile(char fileName[])
{
/*
**Assuming the following data in the text file:
**LastName FirstName PhoneNumber DateOfBirth
**(repeating 50 times)
*/
//setting local variable
filename = fileName;

//declaring variables
int i, j;

//opening file
ifstream file (filename.c_str());

//getting the data and copying it to local variables
while( file.good() )
{
    for(i=0; i<50; i++)
    {
        file >> lastName[i];
        file >> firstName[i];
        file >> phone[i];
        for(j=0; j<10; j++)
            file >> dateOfBirth[i][j];
        file.ignore(100, '\n');
    }
}

//closing file
file.close();
}

//getPhoneNumber function, returns phone number for particular person
int phoneBook::getPhoneNumber(string lastN, string firstN)
{
//declaring variables
int i;
int index;
int phoneNum;

//finding matching first name, last name and its index
for(i=0; i<50; i++)
{
    if(lastName[i] == lastN && firstName[i] == firstN)
        index = i;
}

//returning phone number for matching name
phoneNum = phone[index];
return phoneNum;
}

//getDOB function, returns date of birth for a particular person
void phoneBook::getDOB(std::string lastN, std::string firstN, char testDOB[])
{
//declaring variables
int i;
int index;

//finding matching first name, last name and its index
for(i=0; i<50; i++)
{
    if(lastName[i] == lastN && firstName[i] == firstN)
        index = i;
}

//sets the char array which was passed by reference to the date
//of birth of the matched index
for(i=0; i<11; i++)
    testDOB[i] = dateOfBirth[index][i];
 }

//printPhone_DOB function, prints phone number and date of birth for a
//given person
void phoneBook::printPhone_DOB(std::string lastN, std::string firstN)
{
//declaring variables
int i;
int index;

//finding matching first name, last name and its index
for(i=0; i<50; i++)
{
    if(lastName[i] == lastN && firstName[i] == firstN)
        index = i;
}

//printing phone number and date of birth
cout << "Phone number: " << phone[index] << endl;
cout << "Date of birth: ";
for(i=0; i<10; i++)
    cout << dateOfBirth[index][i];
cout << endl;
}

//printNamesForDOB function, print names of people with birthdays on
//given month
void phoneBook::printNamesForDOB(char month[])
{
//declaring variables
int i;

//printing names of people with birthdays in given month
cout << "The folowing people have birthdays in month number " << month << ":" << endl;
for(i=0; i<50; i++)
{
    if(dateOfBirth[i][3] == month[0] && dateOfBirth[i][4] == month[1])
    {
        cout << "Last name: " << lastName[i] << endl;;
        cout << "First Name: " << firstName[i] << endl;
        cout << endl;
    }
}
}

//saveData function, save data back to the local file
void phoneBook::saveData()
{
/*
**Assuming the following data scheme will be put:
**LastName FirstName PhoneNumber DateOfBirth
**(repeating 50 times)
*/

//opening file, deleting its contents
fstream file (filename.c_str(), fstream::out | fstream::trunc); 

//declaring variables
int i, j;

//re-writting new data to file
for(i=0; i<50; i++)
{
    file << lastName[i];
    file << " ";
    file << firstName[i];
    file << " ";
    file << phone[i];
    file << " "; 
    for(j=0; j<10; j++)
        file >> dateOfBirth[i][j];
    file << " ";
    file << endl; //end line
}

//closing file
file.close();
}

带主要功能的文件

//Problem 2.cpp
#include "phoneBook.h"
#include <iostream>
using namespace std;

int main()
{
 return 0;
}

感谢您的时间。

1 个答案:

答案 0 :(得分:3)

您已声明static个班级的phoneBook个成员,但您尚未提供这些成员的定义。

您需要在.cpp文件中提供如下定义:

string phoneBook::lastName[50];
string phoneBook::lastName[50];
string phoneBook::firstName[50];
int phoneBook::phone[50];
char phoneBook::dateOfBirth[50][10];

我还注意到你的构造函数声明局部变量,其名称与那些成员变量相同。这可能不是你想要做的,因为局部变量一旦被声明的函数退出就会消失。