Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax error with overloaded constructor 1

Status
Not open for further replies.

CaKiwi

Programmer
Apr 8, 2001
1,294
US
Below is my attempt at my C++ assignment to use a class with overloaded constuctors, one passed variables and the other passed a structure. I get a syntax error atempting to pass a structure. Any help greatly appreciated. Also, feel free to offer constructive criticism on any aspect of the program.
TIA

#include <iostream.h>

#define MAXNUMS 3

struct T
{
char name[50];
int s[MAXNUMS];
} info;

char n[50];
int nums[MAXNUMS];

class numbers
{
int sum;
double average;
void input(char *n, int *nums)
{
cout << &quot;Enter Name: &quot;;
cin.getline(n,50);
for (int i=0; i<MAXNUMS; i++)
{
cout << &quot;Enter number &quot; << i+1 << &quot;: &quot;;
cin >> nums;
}
}
void calc(int *nums)
{
sum = 0;
for (int i=0;i<MAXNUMS;i++)
{
sum += nums;
}
average = sum / (double)MAXNUMS;
}
void output(char *n)
{
cout << &quot;Name: &quot; << n << endl;
cout << &quot;Sum: &quot; << sum << endl;
cout << &quot;Aver: &quot; << average << endl;
}
public:
numbers(struct T *x)
{
input(x->name,x->s);
calc(x->s);
output(x->name);
}
numbers(char *n, int *nums)
{
input(n,nums);
calc(nums);
output(n);
}
};

main ()
{
class numbers (n, nums);
class numbers (&info); // SYNTAX ERROR
return 0;
}
CaKiwi
 
You need to define your class objects like

numbers num1(n, nums);
numbers num2(&info);

for example.

VC++ thinks you're defining a new local class. :) Hope that this helped! ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top