表达式必须是可修改的左值指针到字符参数
这是整个代码。编译后我收到以下错误:
错误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;
}
This is the whole code. Upon compiling I get the error below:
error LNK2019: unresolved external symbol "void __cdecl CandyBarFunc(struct CandyBar &,char const *,double,int)" (?CandyBarFunc@@YAXAAUCandyBar@@PBDNH@Z) referenced in function _wmain
fatal error LNK1120: 1 unresolved
externals
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于
name
被定义为char name[40]
,因此您无法编写试图更改astruct.name = aname
的地址>名称数组。但数组的地址不能改变。因此出现了错误。这样做:
strcpy(astruct.name, aname);
更好的是,将
CandyBar
定义为,现在您可以编写:
astruct.name = aname;
>Since
name
is defined aschar name[40]
, you cannot writeastruct.name = aname
which is trying to change the address ofname
array. But address of an array cannot be changed. Hence the error.Do this:
strcpy(astruct.name, aname);
Better yet, define
CandyBar
as,Now you can write :
astruct.name = aname;
您不能编写
char * aname = "Millenium Falcon"
,因为"Millenium Falcon"
是一个(const char *)
,一个不可修改的是记忆的。如果可以的话,更改您的函数签名以接受const char * aname
。或者使用 std::string 代替,毕竟你正在编写 C++。You cannot write
char * aname = "Millenium Falcon"
, because"Millenium Falcon"
is a(const char *)
, a non-modifiable are of memory. Change your function signature to accept aconst char * aname
if you can. Or use a std::string instead, you're writing C++ after all.您将 CandyBar.name 定义为数组,这与 char 指针不同。您必须使用 strcpy 之类的东西来代替赋值语句。如果只使用 STL 字符串会更好。
根据您的评论问题,请参阅此处。
You have CandyBar.name defined as an array, which is not the same as a char pointer. You would have to use something like strcpy instead of the assignment statement. It would be even better to just use STL strings.
As per your comment question see here.
正如问题目前的措辞,缺少的是;在 struct CandyBar 的右大括号之后。
As the question is currently phrased, what is missing is the ; after the closing brace of struct CandyBar.
使用这个:
编辑:并回答您完全改变的问题:
“char * aname”与“const char * aname”不同。您转发声明一个(它给出未解析的外部),然后实现另一个,它永远不会被调用。
Use this:
EDIT: And in response to your completely changed question:
"char * aname" is not the same as "const char * aname". You forward declare one (which gives the unresolved external) and then implement the other, which is never called.