我正在尝试根据与它们关联的字符串为三个指针变量创建一种排序算法。但是,每次尝试运行程序时,经过第一个用户数据入口点后,都会引发分段错误(核心转储)错误。我在许多站点上进行了研究,但未能找到有效的答案。我相信这是我的指针变量的内存分配错误,但是我不知道如何解决它或它在哪里。我应该怎么做才能缓解这种情况?这是一些示例代码:
//new data type
struct Balloon{
string message=""; //give the balloon object a message and a color
string color="";
};
/// main program
int main (void) {
//Instantiate three Balloon objects.
Balloon *front, *middle, *end, *spare;
front, middle, end, spare = new Balloon; //allocate storage for these variables.
//Ask the user what the messages and colors of the balloons are, and set those values to the pointer variables.
//first balloon
cout << "First balloon text: ";
cin >> front->message; //here is when the error is thrown
cout << "Color: ";
cin >> front->color;
我是Ubuntu和C ++的新手,以前只使用过Java,因此对于我可能不经意间犯下的任何严重错误表示歉意。预先感谢您的帮助!
front, middle, end, spare = new Balloon; //allocate storage for these variables.