0

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;
}
BillGates
  • 11
  • 1
  • 1
  • 3
  • 1
    What does the CandyBar struct look like? – Erik Feb 15 '11 at 16:45
  • 1
    You need to supply the definition of `CandyBar`! – ltjax Feb 15 '11 at 16:46
  • Edited to show the structure of CandyBar – BillGates Feb 15 '11 at 16:52
  • Why is char[30] not the same as char pointer? Aren't arrays and pointers basically the same thing? – BillGates Feb 15 '11 at 16:53
  • 1
    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 Feb 15 '11 at 16:58
  • I've edited the post to show the entore code. I've made all the changes to it like you all suggested, but I still get compilation error specified above. – BillGates Feb 15 '11 at 17:05
  • As the compiler said - you are missing a ';'. – sstn Feb 15 '11 at 17:08
  • Is there a reason you want to use a char[40] and not a string? You'll save yourself a lot of agony and manual copying. – Enno Feb 15 '11 at 17:11
  • It's book's specifications. "c++ primer plus" I'm reading myself. – BillGates Feb 15 '11 at 17:17
  • Now I'm getting another error... The original post edited with fresh code and new error. Is it ambigious function error? – BillGates Feb 15 '11 at 17:31
  • I'd say it is char * vs const char *. – sstn Feb 15 '11 at 17:39
  • Wow, didn't notice that. Thanks – BillGates Feb 15 '11 at 17:48

5 Answers5

4

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;

Nawaz
  • 353,942
  • 115
  • 666
  • 851
1

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.

Enno
  • 1,736
  • 17
  • 32
1

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.

Community
  • 1
  • 1
GWW
  • 43,129
  • 11
  • 115
  • 108
1

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.

Erik
  • 88,732
  • 13
  • 198
  • 189
1

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

Henke
  • 41
  • 2