表达式必须是可修改的左值指针到字符参数

发布于 2024-10-17 09:56:48 字数 1205 浏览 5 评论 0原文

这是整个代码。编译后我收到以下错误:

错误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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你另情深 2024-10-24 09:56:48

由于 name 被定义为 char name[40],因此您无法编写试图更改 astruct.name = aname 的地址>名称数组。但数组的地址不能改变。因此出现了错误。

这样做: strcpy(astruct.name, aname);

更好的是,将 CandyBar 定义为,

struct CandyBar
{
     std::string name;
     double weight;
     int calories;
};

现在您可以编写: astruct.name = aname; >

Since name is defined as char name[40], you cannot write astruct.name = aname which is trying to change the address of name array. But address of an array cannot be changed. Hence the error.

Do this: strcpy(astruct.name, aname);

Better yet, define CandyBar as,

struct CandyBar
{
     std::string name;
     double weight;
     int calories;
};

Now you can write : astruct.name = aname;

§对你不离不弃 2024-10-24 09:56:48

您不能编写 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 a const char * aname if you can. Or use a std::string instead, you're writing C++ after all.

温柔女人霸气范 2024-10-24 09:56:48

您将 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.

知足的幸福 2024-10-24 09:56:48

正如问题目前的措辞,缺少的是;在 struct CandyBar 的右大括号之后。

As the question is currently phrased, what is missing is the ; after the closing brace of struct CandyBar.

一杯敬自由 2024-10-24 09:56:48

使用这个:

  strncpy(astruct.name, aname, sizeof(astruct.name)); 
  astruct.name[sizeof(astruct.name)-1] = 0;

编辑:并回答您完全改变的问题:

“char * aname”与“const char * aname”不同。您转发声明一个(它给出未解析的外部),然后实现另一个,它永远不会被调用。

Use this:

  strncpy(astruct.name, aname, sizeof(astruct.name)); 
  astruct.name[sizeof(astruct.name)-1] = 0;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文