好的,所以我对c ++很新,这是我第一次尝试使用矢量。我的目标是将对象存储到矢量中。我试图按照这个youtube教程进行操作:
http://www.youtube.com/watch?v=iPlW5tSUOUM
我觉得除了跑步之外,我的情况几乎相同。
我只是不断收到错误,但它不会运行。任何帮助,将不胜感激 :) 也许它有点小,但我现在已经检查了一段时间,我看不到任何东西。
错误: 1> c:\ users \ user \ desktop \ vector objects c ++ \ testvectorobjects \ testvectorobjects \ main.cpp(61):error C3867:' Employees :: getSalary':函数调用缺少参数列表;使用'& Employees :: getSalary'创建指向成员的指针
1> c:\ users \ user \ desktop \ vector objects c ++ \ testvectorobjects \ testvectorobjects \ main.cpp(61):error C2679:binary'<<<' :找不到哪个运算符采用类型'重载函数'的右手操作数。 (或者没有可接受的转换)
1> c:\ program files(x86)\ microsoft visual studio 11.0 \ vc \ include \ 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 :(得分:3)
'Employees::getSalary': function call missing argument list;
这似乎很清楚:getSalary
是一个函数,当你调用一个函数时需要一个参数列表:
cout << "Employee Salary: " << newStaff[i].getSalary() << endl;
^^ ^
,同样适用于对getID
和getName
的调用。
修复那个也应该修复第二个错误;或者可能是endl
。
答案 1 :(得分:2)
一旦你了解了一些C ++术语,我认为错误是非常自我解释的。第二部分(use '&Employees::getSalary' to create a pointer to member
)实际上会让你感到困惑,因为编译器在讨论一个完全不相关的C ++功能,它认为你可能会尝试使用它。
让我们看看:
'Employees :: getSalary':函数调用缺少参数列表
要调用一个函数,你必须用括号指定参数列表,即使你根本没有参数!
cout << "Employee Salary: " << newStaff[i].getSalary() << end;
也就是说,在这里和那里添加一些()
。
答案 2 :(得分: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;
^^
答案 3 :(得分:0)
此:
newStaff[i].getName
需要这样:
newStaff[i].getName()
等等。