【发布时间】:2014-04-06 10:50:44
【问题描述】:
好的,所以我对 c++ 还很陌生,这是我第一次尝试使用向量。我的目标是将对象存储到向量中。我正在尝试遵循这个 youtube 教程:
http://www.youtube.com/watch?v=iPlW5tSUOUM
我认为除了他的跑步之外,我的几乎相同。
我只是不断收到错误,它不会运行。任何帮助,将不胜感激 :) 也许是小东西,但我已经检查了一段时间了,我什么也看不到。
错误: 1>c:\users\user\desktop\vector 对象 c++\testvectorobjects\testvectorobjects\main.cpp(61): 错误 C3867: 'Employees::getSalary': 函数调用缺少参数列表;使用 '&Employees::getSalary' 创建指向成员的指针
1>c:\users\user\desktop\vector 对象 c++\testvectorobjects\testvectorobjects\main.cpp(61): 错误 C2679: 二进制 '
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\ostream(695): 可能是 'std::basic_ostream<_elem> &std::operator (std: :basic_ostream<_elem> &,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;
}
}
//员工.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 行)。
标签: c++ visual-studio oop object vector