0

クラスのオブジェクトの作成に問題があり、定義されたデータ メンバーを使用してクラス オブジェクトを作成しようとするとエラーが発生します。

// class header for employee
#pragma once
#include <string>
#include <iostream>
class Employee
{
private:
    std::string name;
    int empnum;
    std::string address;
    std::string phone;
    double hourwage;
    double hoursworked;
public:
    Employee(void);
    Employee(int, std::string , std::string, std::string, double, double);
    double gethourwage () const;
    double gethoursworked () const;
    double printcheck () const;
    std::string getphone() const;
    std::string getname() const;
    std::string getaddress() const;
};
// end of header

// employee class.cpp
#include "Employee.h"
#include <string>
#include <iostream>

Employee::Employee(void)
{
    int empnum = 0;
    double hourwage = 0.0;
    double hoursworked = 0.0;
}
Employee::Employee(int num, std::string nme, std::string addres, std::string phon, double hourpay, double hrswrked)
{
    num = empnum;
    nme = name;
    addres = address;
    phon = phone;
    hourpay = hourwage;
    hrswrked = hoursworked;
}

double Employee::gethourwage() const
{
    return hourwage;
}
double Employee::gethoursworked() const
{
    return hoursworked;
}
double Employee::printcheck() const
{
    double pay = 0.0;
    double hrspay = hourwage;
    double hrswork = hoursworked;
    return hoursworked;

}
// end of employee.cpp
// main
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>
#include <string>
#include "Employee.h"

using namespace std;
int main( )
{
    int num1 = 10210;
    double hourwage = 20.2;
    double hourworked = 32.3;
    string steve;
    Employee emp(num1, steve, 58s200w, 90210, hourwage, hourworked);
    cout << "" << emp.getaddress();

    system("PAUSE");
    return 0;
} // end of main

"Employee emp(num1, steve, 58s200w, 90210, hourwage, hourworked);" 一番下にあるのは、私が問題を抱えている行です。間違った順序で入力しているのか、それとも何か他のものなのかわかりません。

助けてくれてありがとう。

4

2 に答える 2

3

文字列は二重引用符で囲む必要があります。

Employee emp(num1, "steve", "58s200w", "90210", hourwage, hourworked);

編集 1 ( @Alex が提案したように、const char *との違いを説明しようとしました)std::string

上記のスニペットのリテラル文字列はconst char *、C 言語から継承された低レベルのエンティティである 型です。タイプ の連続した値を保持するメモリ領域へconst char *ポインタconst charです。

std::stringは、より «ユーザーフレンドリーな» API を提供し、C スタイル文字列の問題 (動的割り当てや自動メモリ クリーンアップなど) を解決することを目的とした、C スタイル文字列の高レベル ラッパーです。

std::stringクラスにはパラメーターを受け取るコンストラクターがあるためconst char *、コンストラクターに渡されるリテラル文字列Employeeは暗黙的に に変換されstd::stringます。

于 2013-01-26T10:50:47.567 に答える
1

あなたの Employee クラスのコンストラクタは

Employee::Employee(int num, std::string nme, std::string addres, std::string phon, double hourpay, double hrswrked)

そして、あなたはそれを次のように呼んでいます

Employee emp(num1, steve, 58s200w, 90210, hourwage, hourworked);

違いを直接確認できます。従業員クラスのコンストラクターは、2 番目、3 番目、4 番目の引数を文字列として想定しています。したがって、これらの引数を文字列として渡す必要があります。文字列を時給、定義した時間労働変数として宣言するか、上記で説明した名前のない引数を渡す必要があります。

于 2013-01-26T11:06:26.200 に答える