compiler error

C

Comcast

Can anyone help:
When I compile this code, I'm getting

[{Linker error] undefined reference to 'getData(WeatherStats*,int)'

I'm not sure what is causing the error.

Thanks
Art



// This program creates a weather stat structure
// and calculates avg. monthly rainfall, total
// annual rainfall, annual highest and lowest temps
// and the months they occurred and the avg montly temp/
// using the computed avg of all months


#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes

void holdHigh(int [] ,int);

enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

//function prototype for displayMonth
void displayMonth(int);

struct WeatherStats
{
float rainFall; //total rainfall by month
float highTemp; //high temp for month
float lowTemp; //low temp for month
float avgTemp; //avg temp for month
};
//function prototype for data getData

void getData(WeatherStats[], int);

void getMonth(int);

int main()
{
const int NUM_MONTHS = 12; //number of months
WeatherStats currentYear[NUM_MONTHS]; // array of WeatherStats
//Month currentMonth; //define enum for type Month

getData(currentYear, NUM_MONTHS); //function call to getData


system("PAUSE");
}




void getData(WeatherStats currentMonth, int theMonth)
{
WeatherStats tempWeatherStats[theMonth]; //temp for holding
structure variables
for (int x=0;x < theMonth; x++)
{
cout << "Please enter the rainfall for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].rainFall;
cout << "Please enter the high temperature for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].highTemp;
cout << "Please enter the low temperature for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].lowTemp;
tempWeatherStats[x].avgTemp = (tempWeatherStats[x].highTemp +
tempWeatherStats[x].lowTemp)/2;
}

}

//function to display month name

void displayMonth(int month)
{
switch(month)
{
case JAN :cout << "January";
break;
case FEB :cout << "February";
break;
case MAR :cout << "March";
break;
case APR :cout << "April";
break;
case MAY :cout << "May";
break;
case JUN :cout << "June";
break;
case JUL :cout << "July";
break;
case AUG :cout << "August";
break;
case SEP :cout << "September";
break;
case OCT :cout << "October";
break;
case NOV :cout << "November";
break;
case DEC :cout << "December";
break;
}
}
 
P

pauldepstein

Can anyone help:
When I compile this code, I'm getting

[{Linker error] undefined reference to 'getData(WeatherStats*,int)'

I'm not sure what is causing the error.

Thanks
Art

// This program creates a weather stat structure
// and calculates avg. monthly rainfall, total
// annual rainfall, annual highest and lowest temps
// and the months they occurred and the avg montly temp/
// using the computed avg of all months

#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes

void holdHigh(int [] ,int);

enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

//function prototype for displayMonth
void displayMonth(int);

struct WeatherStats
{
float rainFall; //total rainfall by month
float highTemp; //high temp for month
float lowTemp; //low temp for month
float avgTemp; //avg temp for month};

//function prototype for data getData

void getData(WeatherStats[], int);

void getMonth(int);

int main()
{
const int NUM_MONTHS = 12; //number of months
WeatherStats currentYear[NUM_MONTHS]; // array of WeatherStats
//Month currentMonth; //define enum for type Month

getData(currentYear, NUM_MONTHS); //function call to getData

system("PAUSE");

}

void getData(WeatherStats currentMonth, int theMonth)
{
WeatherStats tempWeatherStats[theMonth]; //temp for holding
structure variables
for (int x=0;x < theMonth; x++)
{
cout << "Please enter the rainfall for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].rainFall;
cout << "Please enter the high temperature for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].highTemp;
cout << "Please enter the low temperature for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].lowTemp;
tempWeatherStats[x].avgTemp = (tempWeatherStats[x].highTemp +
tempWeatherStats[x].lowTemp)/2;
}

}

//function to display month name

void displayMonth(int month)
{
switch(month)
{
case JAN :cout << "January";
break;
case FEB :cout << "February";
break;
case MAR :cout << "March";
break;
case APR :cout << "April";
break;
case MAY :cout << "May";
break;
case JUN :cout << "June";
break;
case JUL :cout << "July";
break;
case AUG :cout << "August";
break;
case SEP :cout << "September";
break;
case OCT :cout << "October";
break;
case NOV :cout << "November";
break;
case DEC :cout << "December";
break;
}
}

The problem may not be c++ - related. It may be a systems/ platform
issue etc. It's often that way with linker errors. Sorry for the
lack of precision but I'm a newbie. I think I have an excellent
suggestion for troubleshooting. Replace getData by an extremely
simple function that uses very simple types (no pointers or arrays)
but perhaps a simple function that returns void and accepts no
parameters.
See whether you still get the linker error (I'd expect you still
will). Then you will know it's not really a c++ issue and will know
at least where to look.

Paul Epstein
 
R

Ron AF Greve

Hi,

Comcast said:
Can anyone help:
When I compile this code, I'm getting

[{Linker error] undefined reference to 'getData(WeatherStats*,int)'

I'm not sure what is causing the error.

Thanks
Art



// This program creates a weather stat structure
// and calculates avg. monthly rainfall, total
// annual rainfall, annual highest and lowest temps
// and the months they occurred and the avg montly temp/
// using the computed avg of all months


#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes

void holdHigh(int [] ,int);

enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

//function prototype for displayMonth
void displayMonth(int);

struct WeatherStats
{
float rainFall; //total rainfall by month
float highTemp; //high temp for month
float lowTemp; //low temp for month
float avgTemp; //avg temp for month
};
//function prototype for data getData

void getData(WeatherStats[], int);

void getMonth(int);

int main()
{
const int NUM_MONTHS = 12; //number of months
WeatherStats currentYear[NUM_MONTHS]; // array of WeatherStats
//Month currentMonth; //define enum for type Month

getData(currentYear, NUM_MONTHS); //function call to getData


system("PAUSE");
}
// ==============> You pass an array not one struct
void getData(WeatherStats currentMonth[], int theMonth)
void getData(WeatherStats currentMonth, int theMonth)
{
WeatherStats tempWeatherStats[theMonth]; //temp for holding
structure variables
for (int x=0;x < theMonth; x++)
{
cout << "Please enter the rainfall for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].rainFall;
cout << "Please enter the high temperature for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].highTemp;
cout << "Please enter the low temperature for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].lowTemp;
tempWeatherStats[x].avgTemp = (tempWeatherStats[x].highTemp +
tempWeatherStats[x].lowTemp)/2;
}

}

//function to display month name

void displayMonth(int month)
{
switch(month)
{
case JAN :cout << "January";
break;
case FEB :cout << "February";
break;
case MAR :cout << "March";
break;
case APR :cout << "April";
break;
case MAY :cout << "May";
break;
case JUN :cout << "June";
break;
case JUL :cout << "July";
break;
case AUG :cout << "August";
break;
case SEP :cout << "September";
break;
case OCT :cout << "October";
break;
case NOV :cout << "November";
break;
case DEC :cout << "December";
break;
}
}


The compiler complaints because you pass an array so it expects a method
with an array or which it can't find. With the definition above it will
compile
(didn't check the rest of the code though).

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
C

Christopher Pisz

Comcast said:
Can anyone help:
When I compile this code, I'm getting

[{Linker error] undefined reference to 'getData(WeatherStats*,int)'

I'm not sure what is causing the error.

Thanks
Art



// This program creates a weather stat structure
// and calculates avg. monthly rainfall, total
// annual rainfall, annual highest and lowest temps
// and the months they occurred and the avg montly temp/
// using the computed avg of all months


#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes

void holdHigh(int [] ,int);

enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

//function prototype for displayMonth
void displayMonth(int);

struct WeatherStats
{
float rainFall; //total rainfall by month
float highTemp; //high temp for month
float lowTemp; //low temp for month
float avgTemp; //avg temp for month
};
//function prototype for data getData

void getData(WeatherStats[], int);

void getMonth(int);

int main()
{
const int NUM_MONTHS = 12; //number of months
WeatherStats currentYear[NUM_MONTHS]; // array of WeatherStats
//Month currentMonth; //define enum for type Month

getData(currentYear, NUM_MONTHS); //function call to getData


system("PAUSE");
}




void getData(WeatherStats currentMonth, int theMonth)
{
WeatherStats tempWeatherStats[theMonth]; //temp for holding
structure variables
for (int x=0;x < theMonth; x++)
{
cout << "Please enter the rainfall for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].rainFall;
cout << "Please enter the high temperature for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].highTemp;
cout << "Please enter the low temperature for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].lowTemp;
tempWeatherStats[x].avgTemp = (tempWeatherStats[x].highTemp +
tempWeatherStats[x].lowTemp)/2;
}

}

//function to display month name

void displayMonth(int month)
{
switch(month)
{
case JAN :cout << "January";
break;
case FEB :cout << "February";
break;
case MAR :cout << "March";
break;
case APR :cout << "April";
break;
case MAY :cout << "May";
break;
case JUN :cout << "June";
break;
case JUL :cout << "July";
break;
case AUG :cout << "August";
break;
case SEP :cout << "September";
break;
case OCT :cout << "October";
break;
case NOV :cout << "November";
break;
case DEC :cout << "December";
break;
}
}


Your function prototype and definition are differant. It is complaining that
it can't find the definition of the getData function with the arguments you
listed. You made the protype take an array and the definition take a single
element. They must match to work. Any time the compiler tells you "undefined
reference" to anything. Just translate that to "I can't find this thing,
where did you put it?" Then ask yourself, what thing is it looking for,
where did I tell it to look for it, where did I put it, and did I tell mr
compiler where I put it?
 
C

Comcast

Thank you everyone, especially Mr. Pisz. I've just begin my journey with
c++ and i'm sure i'll be posting other questions and perhaps one day
actually answering a few.


Art



Christopher Pisz said:
Comcast said:
Can anyone help:
When I compile this code, I'm getting

[{Linker error] undefined reference to 'getData(WeatherStats*,int)'

I'm not sure what is causing the error.

Thanks
Art



// This program creates a weather stat structure
// and calculates avg. monthly rainfall, total
// annual rainfall, annual highest and lowest temps
// and the months they occurred and the avg montly temp/
// using the computed avg of all months


#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes

void holdHigh(int [] ,int);

enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

//function prototype for displayMonth
void displayMonth(int);

struct WeatherStats
{
float rainFall; //total rainfall by month
float highTemp; //high temp for month
float lowTemp; //low temp for month
float avgTemp; //avg temp for month
};
//function prototype for data getData

void getData(WeatherStats[], int);

void getMonth(int);

int main()
{
const int NUM_MONTHS = 12; //number of months
WeatherStats currentYear[NUM_MONTHS]; // array of WeatherStats
//Month currentMonth; //define enum for type Month

getData(currentYear, NUM_MONTHS); //function call to getData


system("PAUSE");
}




void getData(WeatherStats currentMonth, int theMonth)
{
WeatherStats tempWeatherStats[theMonth]; //temp for holding
structure variables
for (int x=0;x < theMonth; x++)
{
cout << "Please enter the rainfall for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].rainFall;
cout << "Please enter the high temperature for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].highTemp;
cout << "Please enter the low temperature for ";
displayMonth(theMonth);
cout <<": ";
cin >> tempWeatherStats[x].lowTemp;
tempWeatherStats[x].avgTemp = (tempWeatherStats[x].highTemp +
tempWeatherStats[x].lowTemp)/2;
}

}

//function to display month name

void displayMonth(int month)
{
switch(month)
{
case JAN :cout << "January";
break;
case FEB :cout << "February";
break;
case MAR :cout << "March";
break;
case APR :cout << "April";
break;
case MAY :cout << "May";
break;
case JUN :cout << "June";
break;
case JUL :cout << "July";
break;
case AUG :cout << "August";
break;
case SEP :cout << "September";
break;
case OCT :cout << "October";
break;
case NOV :cout << "November";
break;
case DEC :cout << "December";
break;
}
}


Your function prototype and definition are differant. It is complaining
that it can't find the definition of the getData function with the
arguments you listed. You made the protype take an array and the
definition take a single element. They must match to work. Any time the
compiler tells you "undefined reference" to anything. Just translate that
to "I can't find this thing, where did you put it?" Then ask yourself,
what thing is it looking for, where did I tell it to look for it, where
did I put it, and did I tell mr compiler where I put it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top