2013-10-08 82 views
-4

我想我正確地聲明瞭所有東西,但是我在main.cpp的第22-26行中得到了未聲明的標識符。我也得到了這個=在我的employee.cpp中12-16行不明確。我也有一個奇怪的表達式,必須有指向employee.cpp行55中工資的對象類型的指針。我非常感謝任何幫助,因爲我的學校目前沒有導師。將成員變量設置爲null不正確

//Employee.h 

using namespace std; 

class Employee { 
private: 
public: 
    string FirstName; 
    string LastName; 
    string DisplayFirstName; 
    string DisplayLastName; 
    string DisplaySalary; 
    string SearchName; 
    float Salary; 
    Employee(string FirstName, string LastName, float Salary) 
    { 
      setFirstName(FirstName); 
      setLastName(LastName); 
      setSalary(Salary); 
    } 
    string setFirstName(string FirstName); 
    string setLastName(string LastName); 
    float setSalary(float Salary); 
    void ReadFile(ifstream& MyinFile, string FirstName, string LastName, float Salary); 
    string EmployeeSearch(string LastName[], string SearchName); 
    void DisplayEmployee (string DisplayFirstName, string DisplayLastName, string DisplaySalary); 
    Employee(); 
}; 

//Employee.cpp 

#include <iostream> 
#include <fstream> 
#include <string> 
#include "Employee.h" 

using namespace std; 

string setFirstName(string FirstName) 
{ 
**FirstName = NULL;** //ambiguous error 
} 
string setLastName(string LastName) 
{ 
**LastName = NULL;** //ambiguous error 
} 
float setSalary(float Salary) 
{ 
Salary = 0.0; 
} 
void ReadFile(ifstream& MyinFile, string FirstName, string LastName, float Salary) 
{ 
char exit_char; 
int MaxSize; 
int count = 0; 

MyinFile.open("employee.dat"); 
    if (!MyinFile) 
    { //no 
     cout << "Can't open input file." << endl; //Tests the right file. 
     char exit_char;       //End Program 
     cout << "Press any key to exit" << endl; 
     cin >> exit_char; 
    } 
    for(count = 0; count < MaxSize; count++) 
    { 
     MyinFile >> LastName[count]; 
     MyinFile >> FirstName[count]; 
     MyinFile >> **Salary[count];** //error 
    } 
MyinFile.close(); 
} 
string EmployeeSearch(string LastName[], string FirstName[], float Salary, string SearchName, string DisplayFirstName, string DisplayLastName, string DisplaySalary) 
{ 
    cout << "Please enter the name of the employee you would like to search." << endl; 
    cin >> SearchName; 

    for (int i = 0; i < 10; i++) 
    { 
     if (LastName[i] == SearchName) 
    { 
     DisplayFirstName = FirstName[i]; 
     DisplayLastName = LastName[i]; 
     DisplaySalary = **Salary[i];** //error 
    } 
    else 
     cout << "Could not find employee." << endl; 
    } 
}; 
void DisplayEmployee (string DisplayFirstName, string DisplayLastName, string DisplaySalary) 
{ 
cout << DisplayFirstName << " "; 
cout << DisplayLastName << " "; 
cout << DisplaySalary << endl; 
}; 

//Main.cpp 
#include <iostream> 
#include <fstream> 
#include <string> 
#include "Employee.h" 

using namespace std; 

const int MaxSize = 100; 

int main() 
{ 
char Redo;   //Input a character to redo the program 
ifstream MyinFile; 
cout << "Your Salary Machine\n\n"; 
Employee Employee; 
Employee.ReadFile(**MyinFile, FirstName, LastName, Salary**); //undeclared identifier error 
do 
{ 
    Employee.EmployeeSearch(**LastName[], SearchName**); //undeclared identifier error 
    Employee.DisplayEmployee(**DisplayFirstName,DisplayLastName,DisplaySalary**); //undeclared identifier error 
    //Asks user if they want redo the program 
    cout << "Would you like to redo the program?\n"; 
    cout << "Please enter Y or N: \n"; 
    cin >> Redo; 
}while(Redo == 'Y' || Redo == 'y'); 

return 0; 
} 

該程序被寫入讀取具有第一和最後一個名稱的文件,然後工資 然後就可以在誰是該文件中的最後一個名稱輸入,它會顯示姓名和薪水,然後重複。我想用一個構造函數來初始化第一個和最後一個名字爲NULL,然後將薪水初始化爲0.0。我也應該使用get和set成員函數。

下面是錯誤:

------ Build started: Project: Lab3Project, Configuration: Debug Win32 ------ 
Main.cpp 
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(22): error C2065: 'FirstName' : undeclared identifier 
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(22): error C2065: 'LastName' : undeclared identifier 
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(22): error C2065: 'Salary' : undeclared identifier 
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(25): error C2065: 'SearchName' : undeclared identifier 
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(25): error C3861: 'LastName': identifier not found 
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(26): error C2065: 'DisplayFirstName' : undeclared identifier 
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(26): error C2065: 'DisplayLastName' : undeclared identifier 
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(26): error C2065: 'DisplaySalary' : undeclared identifier 

Employee.cpp 
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(12): error C2593: 'operator =' is ambiguous 
     c:\program files\microsoft visual studio 10.0\vc\include\xstring(772): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)' 
     with 
     [ 
      _Elem=char, 
      _Traits=std::char_traits<char>, 
      _Ax=std::allocator<char> 
     ] 
     c:\program files\microsoft visual studio 10.0\vc\include\xstring(767): or  'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)' 
     with 
     [ 
      _Elem=char, 
      _Traits=std::char_traits<char>, 
      _Ax=std::allocator<char> 
     ] 
     while trying to match the argument list '(std::string, int)' 
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(16): error C2593: 'operator =' is ambiguous 
     c:\program files\microsoft visual studio 10.0\vc\include\xstring(772): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)' 
     with 
     [ 
      _Elem=char, 
      _Traits=std::char_traits<char>, 
      _Ax=std::allocator<char> 
     ] 
     c:\program files\microsoft visual studio 10.0\vc\include\xstring(767): or  'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)' 
     with 
     [ 
      _Elem=char, 
      _Traits=std::char_traits<char>, 
      _Ax=std::allocator<char> 
     ] 
     while trying to match the argument list '(std::string, int)' 
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(40): error C2109: subscript requires array or pointer type 
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(55): error C2109: subscript requires array or pointer type 
+0

後實際錯誤。 – chris

+1

你有沒有'#include '? – Joseph

+0

並且向你的代碼添加提示,哪一行是第22行,即標記發生錯誤的行。 –

回答

2
  • 看起來你需要做很多工作來改善你的代碼。首先,你應該把所有的頭文件放在Employee.h中。無論你決定包含Employee.h,它都會很有用,它的所有頭文件也將包含在內,所以你不必再次包含它們。
  • 在C++中,您可以將您的變量定義爲指針或引用。請學習指針(*指針),雙指針 **雙指針)也稱爲數組指針,參考(&引用)和變量(變量)之間的區別。您還應該瞭解如何對指針(如指針)進行取消引用。
  • 瞭解類,函數或變量之間的區別定義及其聲明。這對您當前的分配問題尤爲重要。
  • 你有一個員工類,如果你是一個初學者;頭文件用於構造函數,析構函數,成員變量和成員函數定義。而.cpp文件用於構造函數,成員函數和非成員函數聲明。大多數情況下,析構函數的聲明不包含任何內容,但這是刪除指針的地方。
  • 在C++中你必須管理內存,否則你的指針會出現內存泄漏。所以,無論何時你定義一個指針(*指針),你都應該在你的析構函數中刪除它。您在頭文件中缺少析構函數。你需要定義一個。
  • 您應該像這樣在頭文件中定義和/或聲明構造函數。在構造函數中調用SetFirstName(),setLastName(),setSalary()確實沒有意義。這些方法只能在MAIN類中使用。要設置類成員變量,請執行此操作。

    Employee(string FirstName, string LastName, float Salary) 
    this.FirstName = FirstName; 
    this.LastName = LastName; 
    this.Salary = Salary; 
    

    }

  • 包括一類的析構

    ~Employee(){ 
    } 
    
  • 你設置的功能是爲了只設置成員變量,不應返回任何東西。他們應該是無效的,而不是返回一個字符串。喜歡這個。

  • 設置功能定義。

    void setFirstName(string FirstName);
    void setLastName(string LastName);
    void setSalary(float Salary);

  • 定義並聲明GET函數。

    串的getFirstName(){返回名字;}
    串getLastName(){返回名字;}
    浮動的getSalary(){返回工資;}

  • 在您的.cpp文件,你應該申報的集在您的Employee類中迄今爲止定義的其他成員函數之間起作用。喜歡這個。

    void Employee :: setFirstName(string FirstName){
    FirstName = FirstName;
    }
    void Employee :: setLastName(string LastName){
    LastName = LastName;
    }
    void Employee :: setSalary(float Salary){
    Salary = Salary;
    }

  • 這個Readfile函數讀取這個格式示例的文件;第一行;下一行是John Garry 100.50;邁克爾肖恩250.80等,並將其存儲在僱員的矢量。

    typedef std :: vector < Employee> EmployeeType
    EmployeeType account;

    void ReadFile(ifstream & MyinFile,string FirstName,string LastName,float Salary){
    string st;
    float n;

    while(MyinFile >> st){
    this.setFirstName(st);
    this.setLastName(MyinFile >> st);
    this.setSalary(MyinFile >> n);
    帳戶。的push_back(本);
    }

  • 搜索員工功能。它僅以名字搜索。

  • 您還應該瞭解C++中的數組和數組指針。

    串員工:: EmployeeSearch(串名字,串名字){

    COUT < <「請輸入您想搜索的員工的名字。」 endl; < < endl;
    cin >> SearchName;
    string st =「沒有這樣的員工」;
    string s =「」;

    員工*僱用;

    for(int i = 0; i < account.size(); i ++){
    employ = account.at(i);
    如果(SearchName == employ->的getFirstName()){
    的std :: COUT < < employ->的getFirstName()< < 「」 < < employ-> getLastName()< < 「」 < < employ-> getSalary()< < endl;
    st =「」;

    st.append(employ.getFirstName());
    st.append(s);
    st.append(employ.getLastName());
    st.append(s);

    std :: ostringstream ss;
    ss < < employ.getSalary();
    string so(ss.str());
    st.append(so);
    return st;
    }
    } return st;

    }

1

看這句話在你的main.cpp:

Employee.ReadFile(**MyinFile, FirstName, LastName, Salary**); //undeclared identifier error

從何從FirstNameLastNameSalary?他們從未在int main()的範圍內申報。這就是爲什麼你有未聲明的標識符錯誤。

我不確定爲什麼你的Employee::ReadFile成員函數接受最後三個參數。它應該從文件讀入並設置this.FirstName等,而不是已經傳遞給函數的參數。

這應該更多的讓你在正確的軌道上:

// In header file, replace ReadFile prototype with: 
void ReadFile(ifstream& MyinFile); 

// In implementation file, replace ReadFile with: 

void ReadFile(ifstream& MyinFile) 
{ 
    char exit_char; 
    int MaxSize; 
    int count = 0; 

    MyinFile.open("employee.dat"); 
     if (!MyinFile) 
     { //no 
      cout << "Can't open input file." << endl; //Tests the right file. 
      char exit_char;       //End Program 
      cout << "Press any key to exit" << endl; 
      cin >> exit_char; 
     } 
     for(count = 0; count < MaxSize; count++) 
     { 
      MyinFile >> this->LastName; 
      MyinFile >> this->FirstName; 
      MyinFile >> this->Salary; 
     } 
    MyinFile.close(); 
    } 

// In main.cpp, replace the current call to Employee.ReadFile with: 
Employee.ReadFile(MyinFile); 

這可能不會解決你所有的問題,但它是一個開始。

+0

我認爲僅在我的.h – PhotographyBum

+0

中聲明它們就足夠了。在頭文件中聲明它們會在類本身聲明它們。當你從main.cpp調用成員函數時,你用本地作用域變量調用它。 – Joseph

+0

@BrettHolmes,我已經更新了這個答案,讓你更接近一點。試一試,回報,然後從那裏出發。 – Joseph