gpt4 book ai didi

c++ - vector 问题,C++ 不确定我做错了什么

转载 作者:太空宇宙 更新时间:2023-11-04 15:41:05 26 4
gpt4 key购买 nike

好吧,我对 C++ 还很陌生,这是我第一次尝试使用 vector 。我的目标是将对象存储到一个 vector 中。我正在尝试遵循这个 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 对象 c++\testvectorobjects\testvectorobjects\main.cpp(61): error C2679: binary '<<' : no operator found which takes a right-hand operand of type '重载函数'(或没有可接受的转换)

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;
}

}

//员工.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

//员工.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;
}

最佳答案

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

这看起来很清楚:getSalary 是一个函数,调用函数时需要一个参数列表:

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

对于 getIDgetName 的调用也是如此。

修复第二个错误;或者这可能是由于 endl 的错误输入造成的。

关于c++ - vector 问题,C++ 不确定我做错了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22893002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com