将 Struct 设置为等于返回相同结构的函数
所以我正在进行项目的最后一步,我必须从函数返回一个结构,这样我就可以在 main 中使用它。 可以看到相关代码:
typedef struct SomeProduct {
int itemNum;
char itemName[21];
double unitPrice;
int stockQty;
int restockQty;
struct SomeProduct *next;
} SProduct;
struct llpro {
SProduct data;
struct llpro *next;
};
////////////////////SKIP LINES to identifier
SProduct findItem(struct llpro *head,int num);
///////////////////SKIP LINES to assignment that fails. head is the proper
////////////////// pointeritemspurchased is an int.
SProduct steve;
steve=findItem(head,newv.itemsPurchased[y]);
//////////////////Skip Lines to method
SProduct findItem(struct llpro *head,int num)
{
while(head!=NULL)
{
if(head->data.itemNum==num)
{
SProduct paul;
paul=head->data;
return paul;
}
}
}
每当我尝试编译它时,我都会收到链接器错误,指出它们从未定义。然后,当我取出标识符时,我收到一条消息,指出 steve 和 paul 是不兼容的类型,即使它们都是 SProducts。请帮忙! 我还想澄清一下,我想做的是搜索 Sproducts 的链接列表,并从与我正在搜索的产品共享项目编号的产品中提取信息。链接器错误显示“函数 printsum 中对 'finditem' 的未定义引用
So I'm on the last steps of my project, and I have to return a struct from a function, so I can use it in main.
Relevant code can be seen:
typedef struct SomeProduct {
int itemNum;
char itemName[21];
double unitPrice;
int stockQty;
int restockQty;
struct SomeProduct *next;
} SProduct;
struct llpro {
SProduct data;
struct llpro *next;
};
////////////////////SKIP LINES to identifier
SProduct findItem(struct llpro *head,int num);
///////////////////SKIP LINES to assignment that fails. head is the proper
////////////////// pointeritemspurchased is an int.
SProduct steve;
steve=findItem(head,newv.itemsPurchased[y]);
//////////////////Skip Lines to method
SProduct findItem(struct llpro *head,int num)
{
while(head!=NULL)
{
if(head->data.itemNum==num)
{
SProduct paul;
paul=head->data;
return paul;
}
}
}
Anytime I try to compile it I get linker errors saying that they are never defined. Then when I take out the identifier, I get a message saying that steve and paul are incompatible types, even though they are both SProducts. Please help!
Id also like to clarify, what im trying to do is search through a linked list of Sproducts, and pull the information from the one that shares the item number with the one im searching for. The linker error says "undefined reference to 'finditem' in function printsum
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此代码在 GCC 4.1.2 下使用选项
-Wall -Wextra -std=c99
进行编译:主要更改是使用
1
代替-Wall -Wextra -std=c99
参数的复杂值code>findItem() 并在findItem()
末尾添加return
。两者都不影响任何分配。因此,假设您遇到问题,那么您错误地摘录了代码,并且没有向我们显示导致问题的行。
This code compiles under GCC 4.1.2 with options
-Wall -Wextra -std=c99
:The main changes are using
1
in place of your complex value for the argument tofindItem()
and the addition of areturn
to the end offindItem()
. Neither affects any assignments.So, assuming you have problems, you've mis-excerpted your code and not shown us the lines causing the trouble.