0

I am a c++ beginner and i have trouble compiling the following code the problem is in the statement T a = s where s is a char* containing abcde.

The goal here is to declare an object of T and initializing it with s.

I also have trouble in this line for(int i = 0; i < a(); i++).

I don't know how can i use a constructor or a function named a() to return the size.

This my code :

#include <iostream>
#include <cassert>
using namespace std;
class T{
    int nb ;
    char *pc;
public:
    T(int);
    ~T();
    T(T&);
    char& operator[](int);

    T(const char* s){
        int n ;
        for(int i = 0 ; s[i] != '\0' ;i++ ) n++ ;
        nb = n ;
        pc= new char[nb];
        for(int i=0 ; i < nb ; i++) pc[i] = s[i];
    }


};


    T::T(int k){
        assert(k > 0);
        nb = k;
        pc = new char[nb];
    }
    T ::~T(){
        if( pc != NULL) delete [] pc;
    }
    T::T(T& t){
        nb=t.nb;
        delete[] pc;
        pc = new char[nb];
        for(int i=0 ; i < nb ; i++) pc[i] = t.pc[i];
    }
    char& T::operator[](int index){
        assert(index >=0 && index <= nb);
        return pc[index];
    }



int main(){
    char* s = "abcde";
    T a = s;
    for(int i = 0; i < a(); i++)
        cout << a[i] << " ";
    cout << endl;
    T b = a;
    b[1] = '*';
    b[3] = '*';
    for(int i = 0; i < b(); i++)
    cout << b[i] << " ";
    cout << endl;
    return 0;
}

i get the following error :

invalid initialization of non-const reference of type 'T&' from an rvalue of type 'T'|

  • You should use `std::string` instead of `char` arrays. – Barmar Feb 03 '18 at 11:57
  • the exercise said nothing about using strings – Khalil Kasmi Feb 03 '18 at 11:59
  • Since you tagged C++, you [cannot initialize `char*` with a string literal](https://stackoverflow.com/questions/1524356/c-deprecated-conversion-from-string-constant-to-char). – xskxzr Feb 03 '18 at 12:10
  • In your copy constructor, you are calling delete[] pc on an unitialized value. That's likely to generate a crash. You don't need that line. – KSquared Feb 03 '18 at 12:11

2 Answers2

2

You are misunderstanding the "assignment" when defining the object.

When defining an object like you do with

T a = s;

it's not an assignment, it's an object construction and you need a constructor taking a const char* argument.

If on the other hand you do

T a;
a = s;

then it's an assignment. Of course, this relies on T having a default constructor (a constructor that doesn't take any arguments).


The error you get now is because you need a copy-constructor that takes its argument by constant reference. The definition

T a = s;

is actually equal to

T a = T(s);

And the T(s) creates a temporary object that will be destructed once the full initialization of a is complete. And non-constant references can not be bound to temporary object, so your copy-constructor needs to accept a const T& argument.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • even when using a constructor i still get the error T(const char* s){ int n ; for(int i = 0 ; s[i] != '\0' ;i++ ) n++ ; nb = n ; pc= new char[nb]; for(int i=0 ; i < nb ; i++) pc[i] = s[i]; } – Khalil Kasmi Feb 03 '18 at 12:00
  • @KhalilKasmi ***What*** error do you get? When posting questions about build errors, always copy-paste them in full and complete. Please edit your question to include that. – Some programmer dude Feb 03 '18 at 12:02
  • @KhalilKasmi Updated my answer. You might want to [invest in a couple of good books](https://stackoverflow.com/a/388282/440558) as all of this should be covered in those. – Some programmer dude Feb 03 '18 at 12:07
1

Initialization doesn't use operator=. It uses constructor instead, despite the = sign in syntax. So you need to have constructor with signature T::T(char*). By the way, I strongly recommend making it a const char* to avoid compiler yelling at you for assigning to it string literals.

KSquared
  • 58
  • 7