好吧,所以我相當新的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;
}
如果需要,請務必閱讀錯誤信息,多次。仔細看一下引用的行(在你的案例第61行)。 –