2014-04-06 106 views
0

好吧,所以我相當新的C++和這是我第一次嘗試使用向量。我的目標是將對象存儲到矢量中。我試圖按照此YouTube教程:矢量問題,C++不知道我做錯了什麼

http://www.youtube.com/watch?v=iPlW5tSUOUM

,我認爲我的是幾乎相同的除了他的奔跑。

我只是不斷收到錯誤,它不會運行。任何幫助,將不勝感激:) 也許這是一件小事,但我一直在檢查一段時間,我什麼都看不到。 1:c:\ users \ user \ desktop \ vector objects C++ \ testvectorobjects \ testvectorobjects \ main.cpp(61):錯誤C3867:'Employees :: getSalary':函數調用缺少參數列表;錯誤C3867:'Employees :: getSalary'使用 '&僱員::的getSalary' 來創建一個指針構件

1> C:\用戶\用戶\桌面\矢量對象C++ \ testvectorobjects \ testvectorobjects \ main.cpp中(61):錯誤C2679:二進制「< <':找不到操作符找到'overloaded-function'類型的右側操作數(或者沒有可接受的轉換)

1> c:\ program files(x86)\ microsoft visual studio 11.0 \ vc \包括\ ostream(695):可能是'std :: basic_ostream < _Elem,_Traits> & std :: operator < <>(std :: basic_ostream < _Elem,_Traits> &,const char *)'

我有3個文件:main.cpp中,Employee.h,Employees.cpp

//main.cpp

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

void fillVector(vector<Employees>&); 
//fill vector - fills in Employee Info 
//vector<Employees>& - Employees at the station 
void printVector(const vector<Employees>&); 
//print vector - prints the employee info 
//const vector<Employees>& - employees at the station 

using namespace std; 

    vector<Employees> Staff; 


int main(){ 

    fillVector(Staff); 
    printVector(Staff); 

} 


//filling the employee vector 
void fillVector(vector<Employees> & newStaff){ 

    int id; 
    double salary; 
    string name; 

    cout << "Number of Employees" << endl; 
    int amountEmployees; 
    cin >> amountEmployees; 

    for (int i = 0; i < amountEmployees; i++) { 
     cout << "Enter Employee Id: "; 
     cin >> id; 
     cout << "Enter Employee Salary: "; 
     cin >> salary; 
     cout << "Enter Employee Name: "; 
     cin >> name; 

     Employees newEmployees(id, salary, name); 
     newStaff.push_back(newEmployees); 
     cout << endl; 
    } 
    cout << endl; 

} 

//printing the employee vector 
void printVector(const vector<Employees>& newStaff){ 

    unsigned int size = newStaff.size(); 

    for (unsigned int i = 0; i < size; i++) { 
     cout << "Employee Id: " << newStaff[i].getID << endl; 
     cout << "Employee Name: " << newStaff[i].getName << end; 
     cout << "Employee Salary: " << newStaff[i].getSalary << end; 
     cout << endl; 
    } 

} 

//Employee.h

//Header 

#ifndef EMPLOYEE_H 
#define EMPLOYEE_H 

#include <iostream> 
#include <string> 
using namespace std; 

class Employees{ 

    public: 
     //after 
     //Default Constructor 
     Employees(); 

     //Overload constructor 
     Employees(int, double, string); 

     //Destructor 
     ~Employees(); 

     //Accessor Functions 
     int getID() const; 
     //getId 
     //return int - Id for Employee 
     double getSalary() const; 
     //getSalary 
     //return salary - salary of Employee 
     string getName() const; 
     //getName 
     //return name - Name of Employee 

     //Mutators 
     void setId(int); 
     //setId - for Employee 

     void setSalary(double); 
     //setSalary - for Employee 

     void setName(string); 
     //setName - for Employee 
     // 

     //before 
     //ID 
     //void setEmployeeId(int a){ 
     //employeeId = a; 
     //} 
     ////Salary 
     //void setSalary(double b){ 
     //salary = b; 
     //} 
     ////Name 
     //void setName(string c){ 
     //name = c; 
     //} 
    private: 
     //after 
     //before 
     int employeeId; //id 
     double employeeSalary; //salary 
     string employeeName; //name 
}; 


#endif 

// Employees.cpp

#include "Employee.h" 


Employees::Employees() { 
    employeeName = ' '; 
} 

Employees::Employees(int id, double salary, string name){ 
    employeeId = id; 
    employeeSalary = salary; 
    employeeName = name; 
} 

Employees::~Employees(){ 

} 

int Employees::getID()const{ 
    return employeeId; 
} 

double Employees::getSalary()const{ 
    return employeeSalary; 
} 

string Employees::getName()const{ 
    return employeeName; 
} 

void Employees::setId(int id){ 
    employeeId = id; 
} 

void Employees::setSalary(double salary){ 
    employeeSalary = salary; 
} 

void Employees::setName(string name){ 
    employeeName = name; 
} 
+0

如果需要,請務必閱讀錯誤信息,多次。仔細看一下引用的行(在你的案例第61行)。 –

回答

3
'Employees::getSalary': function call missing argument list; 

這似乎很清楚:getSalary是一個函數,當你調用一個函數,你需要一個參數列表:

cout << "Employee Salary: " << newStaff[i].getSalary() << endl; 
                ^^  ^

,同樣的呼籲getIDgetName

修復,應該也解決第二個錯誤;或者可能是由於endl的錯誤造成的。

+0

謝謝我,新的它會是這樣的。 – user3503320

0

This:

newStaff[i].getName 

需求是這樣的:

newStaff[i].getName() 

等。

2

我認爲這個錯誤是非常自我解釋的,一旦你知道一些C++術語。第二部分(use '&Employees::getSalary' to create a pointer to member)實際上會讓你困惑,因爲編譯器在談論它完全不相關的C++功能,它認爲你可能會嘗試使用它。

讓我們來看看:

「員工::的getSalary」:函數調用缺少參數列表

要調用一個函數,你必須指定參數列表,用括號,即使你根本沒有任何爭論!

cout << "Employee Salary: " << newStaff[i].getSalary() << end; 

也就是說,在這裏和那裏添加幾個()

1

你必須使用函數調用operator()(應用運營商)來調用一個函數

function_name() // call a function named function_name 

這樣的:

cout << "Employee Id: " << newStaff[i].getID() << endl; 
              ^^ 
cout << "Employee Name: " << newStaff[i].getName() << end; 
               ^^ 
cout << "Employee Salary: " << newStaff[i].getSalary() << end; 
                ^^ 
相關問題