0

I am writing c++ program.

This is snippet of main method:

 Student * array = new Student[4]; int i = 0; for(char x = 'a'; x < 'e'; x++){ array[i] = new Student(x+" Bill Gates"); i++; } defaultObject.addition(array, 4); 

This line array[i] = new Student(x+" Bill Gates"); throws an error:

g++ -c -g -MMD -MP -MF build/Debug/Cygwin-Windows/Run.o.d -o build/Debug/Cygwin-Windows/Run.o Run.cpp In file included from Run.cpp:12: Assessment3.hpp:53:39: warning: no newline at end of file Run.cpp: In function `int main(int, char**)': Run.cpp:68: error: no match for 'operator=' in '*((+(((unsigned int)i) * 8u)) + array) = (string(((+((unsigned int)x)) + ((const char*)" Bill Gates")), ((const std::allocator<char>&)((const std::allocator<char>*)(&allocator<char>())))), (((Student*)operator new(8u)), (<anonymous>->Student::Student(<anonymous>), <anonymous>)))' Student.hpp:19: note: candidates are: Student& Student::operator=(Student&) make[2]: Leaving directory `/cygdrive/g/Aristotelis/C++/assessment3' make[1]: Leaving directory `/cygdrive/g/Aristotelis/C++/assessment3' make[2]: *** [build/Debug/Cygwin-Windows/Run.o] Error 1 make[1]: *** [.build-conf] Error 2 make: *** [.build-impl] Error 2 BUILD FAILED (exit value 2, total time: 3s) 

Student class is over here:

#include "Student.hpp" #include <string> using namespace std; Student::Student(){ in = "hooray"; } Student::Student(string in) { this -> in = in; } Student::Student(const Student& orig) { } Student::~Student() { } Student & Student::operator=(Student & student){ if(this != &student){ this->in = student.in; } return *this; } 

Header file is here:

#include <string> using namespace std; #ifndef STUDENT_HPP #define STUDENT_HPP class Student { public: Student(); Student(string in); Student(const Student& orig); virtual ~Student(); Student & operator=(Student & student); // overloads = operator private: string in; }; #endif /* STUDENT_HPP */ 

This part of program creates array of type Student and stores objects of type student. The the array is passed to compare the values according to bubble sort. What might be the problem?

5
  • 2
    Note that your expression x+" Bill Gates" doesn't do what you expect, either. It does not do string concatenation because neither argument is a string. Instead, it advances a pointer to the character array " Bill Gates" by as many positions as the code point of x (e.g. for x=='a' in ASCII, it goes to the 97th character of " Bill Gates", which obviously doesn't exist) and then interprets whatever it finds there as C string. If you are lucky, you get a segmentation fault (or your platform's equivalent). Otherwise you most likely just get garbage. Commented Dec 4, 2011 at 15:34
  • your array is not array of Student* it's array of Student. So array[i] = *(new Student(x+" Bill Gates")); Commented Dec 4, 2011 at 15:35
  • @Yappie: There is no way to delete the pointer returned by new in that statement. It would leak memory every time it's used. Commented Dec 4, 2011 at 15:44
  • Your assignment operator needs to either accept it's argument by const reference, or by value. Not by non-const reference, otherwise, you can't do this: array[i] = Student(), because that's trying to bind a non-const reference to a temporary, which is illegal. Commented Dec 4, 2011 at 15:44
  • @Blastfurnace: Thanks, It's really stupid solution. Commented Dec 4, 2011 at 15:56

2 Answers 2

1

'array' is an array of students declared on the freestore not of array of pointers to students so you can't assign a pointer to them, new returns a pointer to a new location on the freestore. Instead you assign a student to the indexed location.

//no new, you are assigning to a memory block already on the array[i]=student("bob"); 

Also whilst I am here, you can't concatenate a C string and a char like that. You can however use a std::string to do that heavy lifting.

char x='a'; std::string bill("bill gates"); std::string temp=bill+x; 

Finally, you will save a lot of time, if you a vector instead a C array, a vector will manage it's own memory and provides a interface for you to use.

std::vector<student> array(4, student("Bill Gates")); 

Vectors and string are the defacto way of dealing with arrays and string in c++.

Sign up to request clarification or add additional context in comments.

7 Comments

@ucas: And? He said a lot more than that.
Now I get this error: Run.cpp: In function int main(int, char**)': Run.cpp:70: error: expected primary-expression before "array" Run.cpp:70: error: expected ;' before "array"
on the line before line 70, you are missing a semi colon or you have a n incomplete statement
It wants semicolon in this line: std::vector<student> array(4, student("Bill Gates")); before array keyword.
can't really say without seeing the code, you included #include <vector> but usually it is when the preceding statement is malformed.
|
0

In addition to 111111's answer...

The code x+" Bill Gates" is attempting to add a char to a char[], which is undefined. At least one of the operands to the + operator must already be a string. You may want to consider using x + string(" Bill Gates").

1 Comment

Actually, the operation is not undefined (however with the values given in the program, it indeed gives undefined behaviour, unless run on a platform with a very unusual character encoding). x+"Bill Gates" converts x to int and decays the array to a pointer to the first element, and therefore you have just normal pointer arithmetic. For example if x were '\4', the Student object would get initialized with the string "l Gates".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.