0
votes

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;
}
5
What does the CandyBar struct look like? - Erik
You need to supply the definition of CandyBar! - ltjax
Edited to show the structure of CandyBar - BillGates
Why is char[30] not the same as char pointer? Aren't arrays and pointers basically the same thing? - BillGates
No, they're not the same. An array "degenerates" to a pointer when passed as a function call argument. Your name[40] array occupies 40 bytes of memory, and the size information is present (see my answer using sizeof). The char * is simply a pointer to a char, which has no real size information embedded, and which you can choose to treat as a pointer to an array of char without the compiler being able to tell whether this is correct or not. - Erik

5 Answers

3
votes

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;

1
votes

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.

1
votes

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.

1
votes

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

1
votes

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.