- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在通过 C 中的函数初始化结构时遇到问题,而且我似乎找不到这里的问题所在。以下是我的代码的相关部分:
struct Date
{
int nMonth;
int nDay;
int nYear;
};
struct Book
{
char szISBN[10];
char szTitle[75];
char szType[50];
char szPublisher[75];
int nPages;
float fPrice;
int nYearOfPub;
int nStatus;
char szHolder[50];
struct Date dueDate;
};
稍后,在我的一个函数中:
struct Book addNewBook(struct Book *pBooks, int nStock, struct tm *t)
{
char szISBN[10];
char szTitle[75];
char szType[50];
char szPublisher[75];
int nPages;
float fPrice;
int nYearOfPub;
int nStatus;
char szHolder[50];
struct Date dueDate = {t->tm_mon+1, t->tm_mday, t->tm_year+1900};//we will set this to be the current day by default
...
struct Book newBook = {*szISBN, *szTitle, *szType, *szPublisher, nPages, fPrice, nYearOfPub, nStatus, *szHolder, dueDate};
return newBook;
}
我一直收到这个看起来非常简单的错误,但似乎无法修复它。
error: incompatible types when initializing type 'char' using type 'struct Date'
除非我有阅读障碍,否则当我创建 Book 结构时程序顶部的数据类型与我稍后在程序中的方法中初始化新书的位置匹配得很好。我在这里错过了什么?怎么回事?
编辑:这是我使用的解决方案,感谢 REACHUS 链接了另一个帮助我找到解决方案的问题。
struct Book addNewBook(struct Book *pBooks, int nStock, struct tm *t)
{
...
struct Book newBook = {"", "", "", "", nPages, fPrice, nYearOfPub, nStatus, "", dueDate};
strncpy(newBook.szISBN, szISBN, 10);
strncpy(newBook.szTitle, szTitle, 75);
strncpy(newBook.szType, szType, 50);
strncpy(newBook.szPublisher, szPublisher, 75);
strncpy(newBook.szHolder, szHolder, 50);
return newBook;
}
最佳答案
*szHolder
在初始化器中相当于 szHolder[0]
,所以这是一个 char
。
与此相反,struct
字段 char szHolder[50]
是一个数组。数组不能用表达式初始化,它们必须有一个 { .. something .. }
初始化器。这类似于数组也不能赋值的事实。
但是,如果这真的是您收到的诊断信息,那它就不仅仅是神秘的了。
关于c - 结构初始化错误: incompatible types when initializing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26963125/
我有一个自定义类 Custom . public class Custom { private Long id; List ids; // getters and setters } 现在
我有一个 Tree 对象,其中包含 Tree 对象的子对象 (HashMap) 等等。 我需要通过 numericPosition 变量过滤对象。 例如: Tree mapTreeRoot = new
我是编码的新手,在尝试了多种解决方案后,我仍然无法弄清楚为什么我的做法是错误的。这是我的完整代码: public class Student { private String name; pr
我在使用泛型时遇到问题。我不知道如何将 OnCallbackWrapper 传递给 CallbackWrapper 过程。我在以下示例中收到“不兼容类型”错误: unit uTest; interfa
我想实现yin-yang puzzle在 haskell 。这是我的尝试(不成功): -- The data type in use is recursive, so we must have a n
这个问题已经有答案了: What does "Incompatible types: void cannot be converted to ..." mean? (1 个回答) 已关闭2 年前。 我
在以下情况下,我无法理解 Java 泛型的行为。 拥有一些参数化接口(interface),IFace ,以及某个类上的方法,该方法返回扩展此接口(interface)的类,> Class getCl
我成功地将我的日期从 JDateChooser 获取到带有以下行的字符串中: String d1 = ((JTextField)jDateChooser1.getDateEditor().getUi
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我正在编写这段使用大数字的代码: import java.math.*; import java.util.*; import java.lang.*; public class main {
我首先使用 JXL 修改 POI 创建的一个 xls 文件。之后我将尝试使用 POI 读取该文件。在 POIFSFileSystem 创建的那一刻 poFileSystem = new POIFSF
这里是完全的 Java 菜鸟。学校刚刚开学,我正在参加 APCS。我们的老师向我们展示了这个名为 Scanner 的很酷的类(class),但他还没有教过我们。我觉得这很酷,所以我决定进一步研究它。在
我见过很多情况,其中声明了一个字节,但来自类似方法的值intToByte 或 StringToByte 被转换为字节,因为程序员提供了一个十六进制-值,一个整数-或字符串值。 我试图将实际的字节值分配
在这个类中,我想返回整个数组列表,而不是作为单个元素。但是,我在编译时收到错误“不兼容类型”。我在这里做错了什么?感谢您的帮助!! import java.util.ArrayList; public
我想设置一个新的 mysql 数据库从属数据库,运行比主数据库 => 5.0.75 更新版本的 mysql => 5.1.41,据我所知,这通常应该没有问题。然而,事实证明设置复制失败了,因为我在 5
我相信conftest缺少正确的标志,但我无法通过查看mkmf.log的内容来找出问题,这些内容包含在下面。 任何想法将不胜感激! 我正在编译用于 OpenWRT 路由器 (mips) 使用 ruby
我正在尝试实现一个呼吸优先的搜索,用于搜索罗马尼亚城市的人工智能程序。 但是,我在这方面遇到了很多麻烦,最新的错误是 searches.java:153: error: incompatible ty
我有编译错误: Error: incompatible types: Object cannot be converted to String. 在行 String buf = it.next();
private byte[] decode_text(byte[] image) { int length = 0; int offset = 32; for(int i=0;
这个问题在这里已经有了答案: Why won't this generic java code compile? (4 个答案) 关闭 9 年前。 给定这个简单的类: import java
我是一名优秀的程序员,十分优秀!