【发布时间】:2012-02-13 15:48:32
【问题描述】:
这个程序是用 C++ 编写的。我正在尝试使用 void 函数来扩展 Line 结构,该结构由整数长度和指向下一条连接线的指针组成。有一个 void Expand 函数用于将行引用分配给结构中的行指针。新行的大小是当前行的两倍。使用我正在使用的代码,我得到一个 g++ 错误“获取临时 [-fpermissive] 的地址”。谁能建议一种方法,让函数将行引用的有效实例添加到 Line 指针 nextLine?
struct Line
{
int length;
Line* nextLine;
};
Line NewLine(Line& lineRef)
{
Line newLine;
newLine.length = lineRef.length * 2;
return newLine;
}
void Expand(Line& lineRef)
{
//Error here states: Taking address of temporary [-fpermissive]
lineRef.nextLine = &NewLine(lineRef);
}
int main() {
Line line;
Expand(line);
cout << line.length << endl;
cout << line.nextLine->length << endl;
return 0;
}
【问题讨论】: