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 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.
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.
Related
#include<iostream>
#include<array>
#include <algorithm>
using namespace std;
class Array{
//declaring a array
public: int a[4];
public: int num;
public:
Array() {
//initializing a array
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
}
Array(const Array &NewObj){
a = new int[4];
for (int i = 0; i < 4; i++)
a[i] = NewObj.a[i];
}
};
int main()
{
cout<<" DEEP COPY : \n";
cout<<"============ \n";
//creating first object
Array obj1;
//changing value of a array at index 3
obj1.a[3]=15;
//declaring second object which is mapped by object 1
Array obj2(obj1);
cout << "Object 1: \n";
//printing values of a array
for(int i=0;i < (sizeof(obj1.a) / sizeof(obj1.a[0])); i++){
cout<<obj1.a[i];
cout << "\n";
}
cout << "Object 2: \n";
for(int i=0;i < (sizeof(obj2.a) / sizeof(obj2.a[0]));i++){
cout<<obj2.a[i];
cout << "\n";
}
return 0;
}
when creating a deep copy of a variable :
a = new int[4];
I received the error: expression must be a modifiable lvalue at this line.
The primary program's purpose is to make a deep copy of an array.
I started by defining the class and declaring an array.
Then I wrote a constructor () { [native code] } that initialises an array.
Then I wrote a copy constructor () { [native code] } in which I built a new array in which the values could be saved, but it failed with an exception about editable values.
You can get rid of a = new int[4]. Think about why you don't need that line in the regular constructor. Do you actually need to be allocating more memory?
I am trying to insert int array x to int *v. here is my code . please provide me with optimal solutions and the reason behind it.
there is an error in this line. Instead of copying array value its taking garbage value. line v1=x;
class vector
{
int *v;
int size;
public:
vector(int m)
{
v = new int[size = m];
for (int i = 0; i < size; i++)
v[i] = 0;
}
vector(int *a)
{
for (int i = 0; i < size; i++)
v[i] = a[i];
}
int operator *(vector &y)
{
int sum = 0;
for (int i = 0; i < size; i++)
sum += v[i] * y.v[i];
return sum;
}
void disp()
{
for (int i = 0; i < size; i++)
cout << v[i] << " ";
cout << "\n";
}
};
int main()
{
clrscr();
int x[3] = { 1,2,3 };
int y[3] = { 4,5,6 };
vector v1(3);
//v1.disp();
vector v2(3);
v2.disp();
v1 = x;
v1.disp();
//v2=y;
v2.disp();
int r = v1 * v2;
cout << "R = " << r;
getch();
return 0;
}
You forgot to add the assignment operator in your vector class:
vector & operator=(int *a)
{
for (int i = 0; i < size; i++)
v[i] = a[i];
return *this;
}
In the the line
v1=x;
May be, you are expecting it to invoke the second constructor which takes int* as argument. But it won't happen.
It can be seen as Type Conversion from Basic type to Class type. where we expect appropriate constructor will get invoked.
see http://www.hexainclude.com/basic-to-class-type-conversion/
But remember, Constructor will be invoked only once after the creation of object.
Here, in the line
vector v1(3);
the first constructor was already invoked. Then the line
v1=x;
won't invoke the second constructor now.
For every class, = operator is default overloaded . That is the reason why we can easily assign objects to one another.
Therefore, the line v1=x invokes default overloaded assignment = operator.
Here, it treats address of array x i.e., &x[0] as address of class object. As it is not address of vector class object
=> it results a Segmentation fault.
YOUR ANSWER
To assign int array to int pointer i.e., to the member variable int* v of the vector class,
overload assignment operator = inside the class .
or
Initialize the class object to array in first line itself. i.e.,
vector v1=x; // modify the class constructor to have size as a constant.
I am trying to do a simple thing but suddenly stuck in between .
Here in my code I am trying to call a constructor in which i would only pass the length, my first constructor initializes an array of size = length with all elements 0.
Then i am passing the array to the constructor to give the previously defined array its values
here is the sample :
class myvector
{
int *arr;
int length;
public :
myvector(int);
myvector(int *);
};
myvector :: myvector (int len)
{
arr = new int [length = len];
for ( int i=0;i< length;i++)
{
arr[i] = 0;
}
}
myvector :: myvector ( int *ex)
{
for ( int i=0;i< length;i++)
{
cout << ex[i] << " " << length <<" ";
arr[i] = ex[i];
cout << arr[i]<< " ";
}
}
int main()
{
myvector v1(5);
int x[5] = {2,3,4,45,6};
v1 = x;
}
Here in my second constructor length which was defined in first constrcutor lost its values , also array arr loses its values
Did I do something ?
Please elaborate me on this
I don't think you quite get what the circumstances are in which constructors are invoked. The line v1 = x doesn't put the values into the memory allocated during the first constructor call. Rather, it constructs a new myvector (using the second constructor) and copies it into v1. The stuff you do during the first constructor call is lost.
It sounds like you want to define an assignment operator, not a constructor taking an int* argument. Also, in general you should declare single-argument constructors as explicit to prevent this sort of thing from happening accidentally.
First of all, when specifying a size of array like this, you should provide a constant value. See here. And I really encourage you to use std::vector<> for such tasks.
But anyway, like Sneftel mentioned, seems like you want to define an assignment operator (see here for more information about overloading operators in tasks like yours).
Here how I'd implemented it(NOT and ideal solution, but it works, and gives a basic idea):
#include <iostream>
using namespace std;
class myvector
{
int *arr;
int length;
public :
//I completely removed second constructor
myvector(int);
~myvector();
void operator=(const int* otherArray);
void printArray();
};
myvector::myvector (int len)
{
length = len;
arr = new int[length]; // This is the way, how you can handle a non constant array sizes
for ( int i=0;i< length;i++)
{
arr[i] = 0;
}
}
void myvector::printArray()
{
for(unsigned i = 0; i < length; ++i)
cout<<"arr["<<i<<"] = "<<arr[i]<<endl;
}
//Here's an overloaded '=' operator.
//int x[5] = {2,3,4,45,6};
//myvector v;
//v = x; - here this function is called
void myvector::operator=(const int* otherArray)
{
for(unsigned i = 0; i < length; ++i)
arr[i] = otherArray[i];
}
myvector::~myvector()
{
delete []arr; // You should ALWAYS delete what you allocated with new
}
int main()
{
myvector v1(5);
int x[5] = {2,3,4,45,6};
v1 = x;
v1.printArray();
}
This has been destroying me for a while. I'm sure there's a reason for this:
chain operator+(chain c)
{
chain<Object> result;
for (int i = 0; i < length(); i++)
{
result.insert(*(Object*)(memory+(i*sizeof(Object))));
}
for (int i = 0; i < c.length(); i++)
{
result.insert(c[i]);
}
for (int i = 0; i < result.length(); i++) // This for loop successfully shows all objects in result
{
cout << result[i];
}
return result;
}
When the value is returned, ie:
chain<int> a;
cin >> a; // enter "5 6 7 8"
chain<int> b;
cin >> b; // enter "9 10 11 12"
chain <int> c = a+b;
cout << c; // Returns "0 0 7 8 9 10 11 12"
The first two numbers are always 0. I can't figure out why. This only happens when adding two chains together; if I cout a or b, I get all of the values.
I would really appreciate it if anyone has any info to share :)
EDIT**
Full Source
#ifndef CHAIN_H
#define CHAIN_H
#include <iostream>
#include <stdlib.h>
using namespace std;
template <class Object>
class chain
{
public:
chain(){
memorySize = 8;
memory = calloc(memorySize, sizeof(Object));
count = 0;
}
chain(Object item){
memorySize = 8;
memory = calloc(memorySize, sizeof(Object));
count = 0;
insert(item);
}
chain(chain & original){
memorySize = 8;
memory = calloc(memorySize, sizeof(Object));
count = 0;
for (int i = 0; i < original.length(); i++)
{
insert(original[i]);
}
}
~chain(){
free(memory);
}
chain operator+(chain c){
chain<Object> result;
for (int i = 0; i < length(); i++)
{
result.insert(this->operator[](i));
}
for (int i = 0; i < c.length(); i++)
{
result.insert(c[i]);
}
for (int i = 0; i < result.length(); i++)
{
cout << result[i];
}
return result;
}
Object & operator[](int pos){
return *(Object*)(memory+(pos*sizeof(Object)));
}
int length(){
return count;
}
void insert(Object item){
if (count == memorySize)
{
doubleMemory();
}
this->operator[](count) = item;
count++;
}
private:
int count;
int memorySize;
void * memory;
void doubleMemory(){
memorySize *= 2;
memory = realloc(memory, (memorySize*sizeof(Object)));
}
};
template <class Object>
ostream& operator<<(ostream& out, chain<Object>& c){
for (int i = 0; i < c.length(); i++)
{
out << c[i] << " ";
}
}
template <class Object>
istream& operator>>(istream& in, chain<Object>& c){
char ch;
int number = 0;
int sign;
while(ch != '\n')
{
ch = in.get();
if (ch == '-')
{
sign = 1;
}
else if (ch >= '0' && ch <= '9')
{
number *= 10;
number += (ch-48);
}
else if (ch == ' ' || ch == '\n')
{
number = sign == 1? 0 - number : number;
c.insert(number);
sign = 0;
number = 0;
}
}
}
#endif
Here's the code I'm testing against:
#include "chain.h"
using namespace std;
int main(){
chain<int> a, b, c;
chain<int> d(10);
chain<int> e(d);
cin >> a;
cout << endl;
cout << endl;
c = a+d;
cout << c;
}
~
The code you’ve shown isn’t the problem. The real problem is probably either in the copy constructor or the destructor of chain – maybe also in the insert method (or, on C++11, in the move constructor).
(It could also be in Object’s copy constructor but I think that’s unlikely.)
EDIT: Oh my. Don’t write such code in C++. It’s unsafe left, right and center. As long as Object is a POD you should be fine but if it isn’t this code yields undefined behaviour. In particular, it doesn’t call the proper constructors and destructors for the objects you store in your chain.
Furthermore, your copy constructor should take an argument of type chain const& since you’re not modifying the passed chain. This in turn requires that you make your class const correct by providing an appropriate const overload of operator [].
Finally and most glaringly, you violate the rule of three because you don’t implement operator = for your chain. Trying to assign one chain to another will consequently result in double frees.
Generally avoid calloc and free and use a standard container instead, or, if that’s not an option, use new[] plus a smart pointer like boost::shared_array to manage memory (but do not use delete[]).
Another thing, never use using namespace in a header file, it will pollute the namespace and lead to name conflicts in the weirdest places.
Well, I see the following problem regarding the copy-constructor. It seems to me that you want this to act as a copy-constructor:
chain(chain & original){
memorySize = 8;
memory = calloc(memorySize, sizeof(Object));
count = 0;
for (int i = 0; i < original.length(); i++)
{
insert(original[i]);
}
}
But it can't since the declaration of the method is wrong. You are missing the const modifier in the copy argument. If you don't include it, the compiler assumes that you are only declaring a normal constructor which works with a mutable reference, and this is not the copy-constructor you need to define following the
Rule of Three.
Just change this:
chain(chain & original){
to this:
chain(const chain & original){
This will probably solve a wrong memory handling, and hopefully you will get a different output in your program.
I was reading a book on operator overloading in c++ and I encountered the following code:
class Array {
...
public:
Array & operator << ( int x) {
// insert x at the end of the array
}
};
Next It says: that overloading of the form a << x << y << z ;
wll not workSo It suggests that second invocation is treated as :( (a << x)<< y ) << z .so it recommends using return *this;
But I am not getting how return *this functions here? Please help!Here is the entire code:
#include <iostream>
#include <cstdlib>
using namespace std;
class Array {
int *a;
int capacity;
int size;
int incr;
public:
Array (int c=10) {
a = new int[c];
capacity = c;
for (int i=0; i<c; i++) a[i]=0;
size=0;
incr = c;
}
Array &operator << (int x) {
if(size<capacity) a[size++] = x;
else {
int *tmp = new int [capacity+incr];
for (int i=0; i<size; i++) tmp[i]=a[i];
delete[] a;
a = tmp;
a[size++]=x;
capacity = capacity+incr;
}
return *this;
};
int operator [] (int i) {
if(i<size) return a[i];
};
};
int main (int argc, char *argv[]) {
int s = atoi (argv[1]);
Array A (s);
for (int i=0; i<s; i++) A << i << i+1;
for (int i=0; i<s; i++) cout << A[i] << endl;
}
This really has nothing to do with operator overloading. It's called chaining and it's easier to explain using regular member functions. Suppose you defined a member function called insert like this:
Array& insert(int x) {
// insert x at the end of the array
return *this;
}
The return *this will return a reference to the current object so that you can chain calls like this:
Array a;
a.insert(0).insert(1).insert(2);
Which is essentially equivalent to:
Array a;
a.insert(0);
a.insert(1);
a.insert(2);
Each call to insert() will return a reference to the original object, allowing other calls to be made using that returned reference. You can overload the << operator to do the same thing:
Array& operator<<(int x) {
// insert x at the end of the array
return *this;
}
Now you can chain calls like this:
Array a;
a << 0 << 1 << 2;
You may be getting confused because of the spacing of Array &operator <<. The return value of the function is Array&, a reference to the array object.
Here's an example. In your call A << i << i+1, the A << i is called first and a reference to the updated A is returned. Next A << i+1 is called, with that new reference.
Yes everything is ok with your code. operator << in your semantics will and returning refference to same object which called it. You can see same in code of operator << of std::ostream and operator >> of std::istream.