Undeclared variable with an Array of Objects

What expression is expected in this error message?
ERROR: line 44 expected primary-expression before "double"

How do I get main() to recognize functions outside of main?
ERROR: 51 `myCar' undeclared (first use this function)

I would appreciate any help you could provide!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <cstdlib>
#include <iostream>

using namespace std; 

class CarData
    {
    private:
        double year, mileage;
        string make, model, name, phone;
    public:
        CarData(); //constructor
        CarData(double, double, string, string, string, string);

        void setYear(double);
        void setMileage(double);
        void setMake(string);        
        void setModel(string); 
        void setName(string);   
        void setPhone(string); 

        double getYear();
        double getMileage();
        string getMake();
        string getModel();
        string getName();
        string getPhone(); 

        void valid_mileage(double);
        void car_details();
    };             

int main()
{
    int numCar=0, ctr;
    cout << "How many new vehicles are being added to the inventory?\n";
    cin >> numCar;
    CarData car[numCar][6]; //creates an car array of "numCar" amounts of the "CarData4 type
    
    for (ctr = 0; ctr < numCar; ctr++)
    {
        cout << "\n\nVehicle " << ctr +1; // Helps user keep track vehicle #
        cout << "\n\tEnter year #: "; // Input information
        cin << CarData::getYear(double);
        
        //CarData::setYear(double year);
 
    }    

    myCar.car_details(); 
    system("PAUSE");
    return 0;
}

//class definition
CarData::CarData()//variable initializer
    {
    year = 0;
    mileage = 0;
    make = "null";
    model = "null";
    name = "null";
    phone = "null";
    }
CarData::CarData(double year, double mileage, string make, string model, string name, string phone)
{
    CarData::year = year;
    vaild_mileage(mileage);
    CarData::make = make;  
    CarData::model = model;
    CarData::name = name;
    CarData::phone = phone;
}

void CarData::setYear(double year)
{
    CarData::year = year;
}

void CarData::setMileage(double mileage)
{
    vaild_mileage(mileage);
}

void CarData::setYear(string make)
{
    CarData::make = make;
}

void CarData::setModel(string model)
{
    CarData::model = model;
}

void CarData::setName(string name)
{
    CarData::name = name;
}

void CarData::setPhone(string phone)
{
    CarData::phone = phone;
}

double CarData::getYear()
{
    return year;
}

double CarData::getMileage()
{
    return Mileage;
}

string CarData::getMake()
{
    return Make;
}

string CarData::getModel()
{
    return Model;
}

string CarData::getName()
{
    return Name;
}

string CarData::getPhone()
{
    return Phone;
}

void CarData::valid_mileage(double mileage)
{
    if (mileage>=0)
        CarData::mileage=mileage;
    else  {
        CarData::mileage=0;
        cout << "Invalid mileage!\n";
    }
}

void CarData::car_details()
{
    cout << "The current car is a " << year << ' ' << make << ' ' << model
    << "with" << mileage << "miles..\n";
}
What is a "myCar"?

What is different between A and B:
1
2
3
4
5
6
// A
int numCar = 0;
cin >> numCar;

// B
cin << CarData::getYear(double);


Line 38 is an error, unless your compiler has support for non-standard (and IMHO unnecessary) variable length arrays.
i dont know if i could help much but one question, did you write this code from the beginning to end without debuging even once?
because i am apparently getting 31 errors.
are those the only errors you get when you compile? (the ones you wrote)
I'm getting quite a few, but most of them are the same problem (undeclared first use of function) I can't seem to wrap my head around why it says that.
@keskiverto: I took out some of the trash I left behind from yesterday (myCar), but It's still having a problem compiling. Do you know what the error means on line 47?

ERROR Line 47 request for member `getYear' in `car[ctr]', which is of non-class type `CarData[6]'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <cstdlib>
#include <iostream>

using namespace std; 

class CarData
    {
    private:
        double year, mileage;
        string make, model, name, phone;
    public:
        CarData(); //constructor
        CarData(double, double, string, string, string, string);

        void setYear(double);
        void setMileage(double);
        void setMake(string);        
        void setModel(string); 
        void setName(string);   
        void setPhone(string);
         

        double getYear();
        double getMileage();
        string getMake();
        string getModel();
        string getName();
        string getPhone(); 

        void valid_mileage(double);
        void car_details();
    };             

int main()
{
    int numCar=0, ctr;
    double info;
    cout << "How many new vehicles are being added to the inventory?\n";
    cin >> numCar;
    CarData car[numCar][6]; //creates an car array of "numCar" amounts of the "CarData4 type
    
    for (ctr = 0; ctr < numCar; ctr++)
    {
        cout << "\n\nVehicle " << ctr +1; // Helps user keep track vehicle #
        cout << "\n\tEnter year #: "; // Input information
        cin >> info;
        cout << "Entered year is " << car[ctr].getYear() << ".";
        cout << endl;
        
        
        //CarData::setYear(double year);
 
    }    

    system("PAUSE");
    return 0;
}

//class definition

CarData::CarData()//variable initializer
    {
    year = 0;
    mileage = 0;
    make = "null";
    model = "null";
    name = "null";
    phone = "null";
    }
CarData::CarData(double year, double mileage, string make, string model, string name, string phone)
{
    CarData::year = year;
    vaild_mileage(mileage);
    CarData::make = make;  
    CarData::model = model;
    CarData::name = name;
    CarData::phone = phone;
}

void CarData::setYear(double year)
{
    CarData::year = year;
}

void CarData::setMileage(double mileage)
{
    vaild_mileage(mileage);
}

void CarData::setYear(string make)
{
    CarData::make = make;
}

void CarData::setModel(string model)
{
    CarData::model = model;
}

void CarData::setName(string name)
{
    CarData::name = name;
}

void CarData::setPhone(string phone)
{
    CarData::phone = phone;
}

double CarData::getYear()
{
    return year;
}

double CarData::getMileage()
{
    return Mileage;
}

string CarData::getMake()
{
    return Make;
}

string CarData::getModel()
{
    return Model;
}

string CarData::getName()
{
    return Name;
}

string CarData::getPhone()
{
    return Phone;
}

void CarData::valid_mileage(double mileage)
{
    if (mileage>=0)
        CarData::mileage=mileage;
    else  {
        CarData::mileage=0;
        cout << "Invalid mileage!\n";
    }
}

void CarData::car_details()
{
    cout << "The current car is a " << year << ' ' << make << ' ' << model
    << "with" << mileage << "miles..\n";
}
Last edited on
the problem is you tried to allocate a 2d array and you are calling 1D(meaning just an array, single dimension), i do not understand why you need a 2d array btw, also thats not how you create a array on the heap.
i just skimmed through it, let me know if it solves some problems.
Thanks! It did solve some issues! Still working on this one...
It is now compiling, but it locks up when after I input the make data. I ran the debugger and it brought up “An access violation (Segmentation Fault) raised in your program.” Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <cstdlib>
#include <iostream>

using namespace std; 

class CarData //defining class "wrapper"
    {
    private:
        double year, mileage;
        string make, model, name, phone;
    public:
        void setYear(double);//mutator functions
        void setMileage(double);
        void setMake(string);        
        void setModel(string); 
        void setName(string);   
        void setPhone(string);
         

        double getYear();// accessor functions
        double getMileage();
        string getMake();
        string getModel();
        string getName();
        string getPhone(); 

        void valid_mileage(double);
        void car_details();
    };         

    
//setYear will assign a value to the private member year
void CarData::setYear(double tempyear)
{
    year = tempyear;
};
//setMileage will assign a value to the private member mileage
void CarData::setMileage(double tempmileage)
{
    mileage = tempmileage;
}
//setMake will assign a value to the private member make
void CarData::setMake(string tempmake)
{
    make = tempmake;
}
//setModel will assign a value to the private member model
void CarData::setModel(string tempmodel)
{
    model = tempmodel;
}
//setName will assign a value to the private member name
void CarData::setName(string tempname)
{
    name = tempname;
}
//setPhone will assign a value to the private member phone
void CarData::setPhone(string tempphone)
{
    phone = tempphone;
}

double CarData::getYear()
{
    return year;
}

double CarData::getMileage()
{
    return mileage;
}

string CarData::getMake()
{
    return make;
}

string CarData::getModel()
{
    return model;
}

string CarData::getName()
{
    return name;
}

string CarData::getPhone()
{
    return phone;
}

void CarData::valid_mileage(double mileage)
{
    if (mileage>=0)
        CarData::mileage=mileage;
    else  {
        CarData::mileage=0;
        cout << "Invalid mileage!\n";
    }
}

//main function
int main()
{
    CarData customerCar;
    int numCar=0, ctr;
    double tempyear[numCar], tempmileage[numCar];// local variables
    string tempmake[numCar], tempmodel[numCar], tempname[numCar], tempphone[numCar];
    cout << "How many new vehicles are being added to the inventory?\n";
    cin >> numCar;
    if (numCar < 1)
        {
        system ("PAUSE");
        return 0;
        }
    CarData car[numCar]; //creates an car array of "numCar" amounts of the "CarData4 type
    
    for (ctr = 0; ctr < numCar; ctr++)
    {
        cout << "\n\nVehicle " << ctr +1; // Helps user keep track vehicle #
        cout << "\n\tEnter year #: "; // Input information
        cin.ignore();        
        cin >> tempyear[ctr];

        while (tempyear[ctr] < 1910 || tempyear[ctr] > 2014)
            {
            cout << "Invalid year.  Please enter the correct year. ";
            cin.clear();
            cin.ignore();
            cin >> tempyear[ctr];
            }
       
        cout << endl;

        cout << "\n\tEnter make: "; // Input information
        cin.ignore();
        getline (cin, tempmake[ctr]);
        cout << endl;
        
        cout << "\n\tEnter model: "; // Input information
        cin.ignore();
        getline (cin, tempmodel[ctr]);
        cout << "test";
        cout << endl;   

        cout << "\n\tEnter mileage #: "; // Input information  
        cin.ignore();   
        cin >> tempmileage[ctr];
        while (tempmileage[ctr] < 0 || tempyear[ctr] > 1000000)
            {
            cout << "Invalid mileage.  Please enter the correct mileage. ";
            cin.clear();
            cin.ignore();
            cin >> tempmileage[ctr];
            }
        cout << endl;     

        cout << "\n\tEnter owner's first and last name: (ex. John Smith)"; // Input information
        cin.ignore();        
        getline (cin, tempname[ctr]);
        cout << endl;

        cout << "\n\tEnter owner's phone #: (ex. (555)555-5555)"; // Input information      
        getline (cin, tempphone[ctr]);
        cout << endl;
        
    }    

    customerCar.setYear(tempyear[ctr]);
    customerCar.setMileage(tempmileage[ctr]);
    customerCar.setMake(tempmake[ctr]);
    customerCar.setModel(tempmodel[ctr]);
    customerCar.setName(tempname[ctr]);
    customerCar.setPhone(tempphone[ctr]);

    system("PAUSE");
    return 0;
}
Last edited on
You have:
1
2
3
4
5
6
7
int numCar=0;
double tempyear[numCar];
numCar = 3; // simulated input
for (ctr = 0; ctr < numCar; ctr++)
{
  cin >> tempyear[ctr];
}

This does the same:
1
2
3
4
5
double tempyear[0]; // How many elements are on this array?

cin >> tempyear[0]; // error
cin >> tempyear[1]; // error
cin >> tempyear[2]; // error 

I need to input 6 different points of data for a user defined amount of vehicles. Trying to make this easy to read for the employees...
Topic archived. No new replies allowed.