【发布时间】:2014-06-29 15:46:59
【问题描述】:
我遇到了转换错误,实际上不知道如何解决。
我必须使用这些结构并且不知道如何访问 Date 结构的权利。 这是我的代码:
#include <iostream>
#include <string.h>
using namespace std;
struct Date {
short year;
short month;
short day;
};
struct Stuff {
Date birth;
};
struct ListElement {
struct Stuff* person; // Pointer to struct Stuff
struct ListElement* next; // Pointer to the next Element
};
int main() {
short birth_year;
short birth_month;
short birth_day;
cin >> birth_year;
cin >> birth_month;
cin >> birth_day;
ListElement* const start = new ListElement();
ListElement* actual = start;
actual->person = new Stuff();
actual->person->birth.year = new short[sizeof(birth_year)]; // Conversion Error
delete start;
delete actual;
}
来自 GCC 的错误消息:
main.cpp: In function 'int main()':
main.cpp:35:29: error: invalid conversion from 'short int*' to 'short int' [-fpermissive]
actual->person->birth.year = new short[sizeof(birth_year)]; // Conversion Error
【问题讨论】:
-
错误信息在哪里?
-
这段代码没有意义。为什么要尝试将数组分配给单个
short? -
Please read more thoroughly what your compiler tells you!你的标题是错误的。而且你不需要
new()顺便说一句。 -
为您添加了来自 GCC 的错误消息。请阅读它,您的类型不匹配:
short int!=short int*! 始终编译时带有完整警告 (-Wall -Wextra -pedantic) 并处理所有警告! -
delete actual;也是一个错误,因为您已经删除了该指针。这个程序闻起来像“Java 程序员尝试 C++”
标签: c++