2014-04-06 155 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; 
                ^^ 
相关问题