【发布时间】:2011-02-15 16:42:47
【问题描述】:
这是整个代码。编译后出现以下错误:
错误 LNK2019:函数 _wmain 中引用了无法解析的外部符号“void __cdecl CandyBarFunc(struct CandyBar &,char const *,double,int)”(?CandyBarFunc@@YAXAAUCandyBar@@PBDNH@Z)
致命错误 LNK1120: 1 未解决 外在
#include "stdafx.h"
#include <iostream>
using namespace std;
struct CandyBar
{
char name[40];
double weight;
int calories;
};
void CandyBarFunc(CandyBar & astruct, const char * aname = "Millennium Munch", double aweight = 2.85, int acalories = 350);
void CandyBarFunc(const CandyBar & astruct);
int _tmain(int argc, _TCHAR* argv[])
{
CandyBar MyCandyBar;
CandyBarFunc(MyCandyBar, "Hello World Candy Bar", 1.25, 200);
CandyBarFunc(MyCandyBar);
return 0;
}
void CandyBarFunc(CandyBar & astruct, char * aname, double aweight, int acalories)
{
strncpy(astruct.name,aname,40);
astruct.weight = aweight;
astruct.calories = acalories;
}
void CandyBarFunc(const CandyBar & astruct)
{
cout << "Name: " << astruct.name << endl;
cout << "Weight: " << astruct.weight << endl;
cout << "Calories: " << astruct.calories;
}
【问题讨论】:
-
CandyBar 结构是什么样的?
-
您需要提供
CandyBar的定义! -
编辑显示 CandyBar 的结构
-
为什么 char[30] 和 char 指针不一样?数组和指针基本上不是一回事吗?
-
不,它们不一样。当作为函数调用参数传递时,数组“退化”为指针。您的 name[40] 数组占用 40 个字节的内存,并且存在大小信息(请参阅我使用 sizeof 的答案)。 char * 只是一个指向 char 的指针,它没有嵌入真正的大小信息,您可以选择将其视为指向 char 数组的指针,而编译器无法判断这是否正确。跨度>