【发布时间】:2015-04-15 18:46:41
【问题描述】:
我有一个这样的问题: C++: Storing structs in a stack
我的代码:
#include <stdio.h>
#include <stack>
#include <iostream>
#include <string>
using namespace std;
struct adresse{
string info;
};
int main(){
string eingabe;
stack<adresse> Stack1;
cout << "Bitte Ausdruck eingeben: " << endl;
getline( cin, eingabe);
adresse* temp;
temp = new adresse;
temp->info = eingabe[0];
Stack1.push(temp);
return 0;
}
错误是:
reference to type 'const value_type'(aka 'const adresse') could not bind to an
lvalue of type 'adresse *'Stack1.push(temp);
怎么了?
谢谢
汤米
【问题讨论】:
-
你试图将一个指针压入你的堆栈,但它不接受指针。
-
一些提示:始终将 new 与 delete 配对(或委托给智能指针),切勿将指针存储在容器中(除非您需要多态性),不要胡闹并获得通知。 (顺便说一句:你的德语很糟糕)