std :: sting? string没有命名类型错误

时间:2017-05-21 04:26:57

标签: c++ namespaces std

我现在有这两个文件,任何时候我想编译我都会收到错误

  

string未命名类型

Bestellung.h的{​​{1}}行中的

为什么呢?

的main.cpp

std::string name;

Bestellung.cpp

#include "Bestellung.h"
#include <iostream>
using namespace std;

int main()
{
    Bestellung();
    cout << Bestellung{"maki"} << endl;// [maki,10€]
}

Bestellung.h

#include "Bestellung.h"

Bestellung(string bestellV, double preisV = 10){
    name = "bestell V";
    preis = "preis V";
};
string get_name const(Bestellung v) {
    return Bestellung.name;
};
double get_preis const(Bestellung v){
    return Bestellung.preis;
};
ostream& print(ostream&) const {
};

1 个答案:

答案 0 :(得分:0)

您必须使用命名空间限定符。你有:

Bestellung(string,double=10);

你应该:

Bestellung(std::string,double=10);

您还有:

string get_name const {

你应该:

std::string get_name const {

如果您不想在每次使用字符串时指定std命名空间,则可以在开头附近执行此操作:

using std::string;

在头文件中执行此操作是不好的做法,所以我会使用像我先说的完整资格。 纠正此错误后,您需要对ostream执行相同的操作。

详细了解名称空间here