|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#16
|
||||
|
||||
|
FWIW, here's my code, tested with g++ 3.2 on a laptop running RH Linux 8.0. AFAIK it is standard C++ and should work on VC++ with no problems too (can someone verify this for me please, because I can only test it for myself on monday at work - thanks in advance).
Code:
#include <iostream>
using namespace std;
class Student {
public:
int x, y;
Student(int a, int b);
};
Student::Student(int a, int b) {
x = a; y = b;
}
int main(void) {
int num_students;
cout << "Enter # of students: ";
cin >> num_students;
Student *student = new Student[num_students](5, 3);
for (int i = 0; i < num_students; i++) {
cout << "Student #" << i+1 << ". " << student[i].x << ", " << student[i].y << endl;
}
delete [] student;
return 0;
}
Thanks for the vote of confidence 7stud ![]() |
|
#17
|
||||
|
||||
|
Errors on VC++ 6.0 matey
Code:
Student *student = new Student[num_students](5, 3); C:\Program Files\Microsoft Visual Studio\MyProjects\test\test.cpp(22) : error C2538: new : cannot specify initializer for arrays C:\Program Files\Microsoft Visual Studio\MyProjects\test\test.cpp(22) : error C2512: 'Student' : no appropriate default constructor available
__________________
Online Designer Baby Clothes Store FiftyFifty Web Page, advertising space, with 50% profit share Free web development scripts! Tattoo Blog Free graphical wedding tickers |
|
#18
|
||||
|
||||
|
Aw shoot, guess I'd have to declare a constructor with no arguments to make it work with VC++ 6.0. Guess this should work then:
Code:
#include <iostream>
using namespace std;
class Student {
public:
int x, y;
Student();
Student(int a, int b);
};
Student::Student() {
x = 5; y = 3;
}
Student::Student(int a, int b) {
x = a; y = b;
}
int main(void) {
int num_students;
cout << "Enter # of students: ";
cin >> num_students;
Student *student = new Student[num_students];
for (int i = 0; i < num_students; i++) {
cout << "Student #" << i+1 << ". " << student[i].x << ", " << student[i].y << endl;
}
delete [] student;
return 0;
}
Thanks for testing it out ![]() |
|
#19
|
||||
|
||||
|
Yep that works a treat Scorps m8
|
|
#20
|
|||
|
|||
|
Gmorphus,
My apologies. I didn't notice the constructor issue if that's what you were questioning. Last edited by 7stud : March 9th, 2003 at 07:21 PM. |
|
#21
|
|||
|
|||
|
apologies accepted
scorp, that is quite exactly what i meant. but, you will have to add Create(x,y) function to intialize x and y, and to retrieve the info from the user. you will also have to do it in your g++ version. what you have done there is to initalize ALL the x and y's with 3 and 5... i don't think that how it should be... anyway, i think we have covered this thread all around.
__________________
"Gravitation can NOT be responsible for people falling in Love" (one of the most significant characters in the history, can you guess?) Gmorph. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Dynamic Object Name |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|