【问题标题】:vector issue, C++ not sure what i've done wrong矢量问题,C++ 不确定我做错了什么
【发布时间】: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


【解决方案1】:
 'Employees::getSalary': function call missing argument list;

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

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

对于getIDgetName 的调用也是如此。

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

【讨论】:

    【解决方案2】:

    一旦您了解了一些 C++ 术语,我认为该错误可以自我解释。第二部分 (use '&amp;Employees::getSalary' to create a pointer to member) 实际上会让您感到困惑,因为它谈论的是完全不相关的 C++ 功能的编译器,它认为您可能正在尝试使用它。

    让我们看看:

    'Employees::getSalary':函数调用缺少参数列表

    要调用一个函数,你必须用括号指定参数列表,即使你根本没有参数!

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

    也就是到处加几个()

    【讨论】:

      【解决方案3】:

      您必须使用函数调用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;
                                                          ^^
      

      【讨论】:

        【解决方案4】:

        这个:

        newStaff[i].getName
        

        需要这样:

        newStaff[i].getName()
        

        等等。

        【讨论】:

          猜你喜欢
          • 2014-03-23
          • 2017-09-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-04
          • 1970-01-01
          • 2021-09-04
          • 2016-08-01
          相关资源
          最近更新 更多