0

ユーザーに文字列の入力を求めるプロジェクトに取り組んでおり、get 関数と set 関数を使用して文字列を表示するだけです。ただし、実際にユーザーが文字列を入力してから、それらを get および set 関数に渡すという問題があります。ここに私のコードがあります: これは私の Main.cpp です:

#include "stdafx.h"
#include <iostream>
#include "Laptop.h"
#include<string>
using namespace std;
int main()
{
    Laptop Brand;
    string i;
    cout << "Enter your brand of laptop : ";
    cin >> i;
    Brand.setbrand (i);
    return 0;
}

これは私の Laptop.cpp です:

#include "stdafx.h"
#include <iostream>
#include "Laptop.h"
#include <string>
using namespace std;
void Laptop::setbrand(string brand)
    {
        itsbrand = brand;
    }

string Laptop::getbrand()
    {
        return itsbrand;
    }

これは私の laptop.h です:

#include<string>
class Laptop
{
private :
    string itsbrand;

public :
    void setbrand(string brand);
    string getbrand();

};

私の laptop.cpp では、setbrand と getbrand にエラーがあります。getbrand と setbrand は互換性がないと言われています。パラメータを介して文字列を渡していることと関係があると確信しています。何か案は?

4

2 に答える 2

1

You have missed to include the correct namespace in the laptop.h file therefore the compiler cannot find any declared string class in the current (global) namespace. Just put, in the beginning of the file, using std::string;.

On a side note I'd avoid generic

using namespace std;

because it fights the purpose of having namespaces in the first place. It's usually better to specify exactly what kind of class you are using. Therefore:

using std::string;

is better.

于 2013-03-19T01:48:42.080 に答える
1

ここでの適切な修正は、ヘッダー ファイルのstd::string代わりに使用することです。string

class Laptop
{
private :
    std::string itsbrand;

public :
    void setbrand(std::string brand);
    std::string getbrand();
};

持っていない他のファイルとは異なりますusing namespace stdstd::string私は実際にどこでも使用することをお勧めします。それはより安全で、後でより悪い問題からあなたを救います.

于 2013-03-19T01:45:56.100 に答える