我现在有这两个文件,并且每次我要编译时都会出现错误
string
没有命名类型
在Bestellung.h
与std::string name;
所在的行中。
为什么?
main.cpp
#include "Bestellung.h"
#include <iostream>
using namespace std;
int main()
{
Bestellung();
cout << Bestellung{"maki"} << endl;// [maki,10€]
}
百事通
#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 {
};
百事通
#ifndef BESTELLUNG_H
#define BESTELLUNG_H
#include <string>
#include <iostream>
class Bestellung{
std::string name;
std::double preis;
public:
Bestellung(string, double = 10);
string get_name const {
};
double get_preis const {
};
ostream& print(ostream&) const {
};
};
#endif
最佳答案
您必须使用名称空间限定符。你有:
Bestellung(string,double=10);
你应该有:
Bestellung(std::string,double=10);
您还有:
string get_name const {
你应该有:
std::string get_name const {
如果不想在每次使用字符串时都指定
std
命名空间,则可以在开头附近进行此操作:using std::string;
不过,在标头文件中执行此操作不是一个好习惯,因此,我将像我之前说的那样使用完整的限定条件。
纠正此错误后,您需要对
ostream
做同样的事情。阅读有关名称空间here的更多信息。
关于c++ - std::string吗?字符串未命名类型错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44093183/