Hi, coders. I have to overload operators +, *, =. Object L3(default "abc") must be smth like this "aabbcc"(operator*; double each symbol of the string). Then I must concatenate Object2 and Object3(operator+), and initialize Object1(operator = ).
I have strange errors
error C2296: '*' : illegal, left operand has type 'Row *'
error C2297: '*' : illegal, right operand has type 'Row *'

Sorry for my English, I know it's awful. So maybe it will be better if I show the code.

#include "stdafx.h"
#include "row.h"
#include <iostream>
#include <string>
using namespace std;

class Row
{
	private:
		char *data;
		
	public:
		Row();
		Row(char* v);
		Row(Row &);
		
		Row* operator*(Row *c);
		Row* operator+(Row *c);
		Row* operator=(Row *h);

		int Dimension();
		void Out();
};
//*************************/
const int n = 2;
Row::Row()
{
	data = NULL;
}

Row::Row(char* v)
{
	data = v;
}

Row::Row(Row &copy)
{
	data = copy.data;
}

int Row::Dimension()
{
	int res0 = strlen(data);
	return res0;
}

void Row::Out()
{
	cout << "stroka: " << data << endl;
}

Row* Row::operator*(Row *c)
{	
	char *tmp = new char[strlen(data)];
	tmp = data;
	
	int len = strlen(data);
	for(int i = 0; i < len; i++)
	{
		tmp[i*2+1] = tmp[i*2] = data[i];
	}
	
	Row *res0 = new Row(tmp);
	return res0;
}

Row* Row::operator+(Row *c)			
{									
	int len = strlen(data);			
	char *tmp = new char[len+1];	
	strcpy(tmp,data);				
	strcat(tmp,c->data);			

	Row *res1 = new Row(tmp);
	return res1;
}

Row* Row::operator=(Row *h)
{
	Row* res3 = new Row(h->data);
	//res3 = h;
	return res3;
/*********************************/
void main()
{
	Row* L3 = new Row("abc");
	Row* L2(L3);

	Row L1;
	L1 = (L3*L3) + L2;                      // ERRORS

	L3->Out();
	cout << "length L3: " << L2->Dimension() << endl;

	L1.Out();
	cout << "length L1: " << L1.Dimension() << endl;
}

Thanks anyway.

L2 and L3 have type Row* so (L3*L3) + L2 is pointer arithmetic (except * is not valid in pointer arithmetic in that way).

You want to operate on your objects not the pointers to them so a sprinkling of * is required to dereference the pointer into an object *L3

dereference the pointer into an object *L3

Can you write an example, please? I just can imagine smth like (*L3)*(*L3).

Edited 6 Years Ago by Macbeth: n/a

Now you've imagined it write it just like that. If its a pointer deference it using a *

Edited code. Working with references, but now I have "an unhandled win32 exception" (at 0x102d31ea).

#include "stdafx.h"
#include <string>
#include "string.h"
#include <iostream>
using namespace std;

class String 
{
private:
	char* s;

public:
	String();
	String(char* v);
	String(String &d);
	~String();

	int StrLength();
	void StrShow();

	String& operator*(const String& s1)const;
	String& operator+(const String& s1)const;
	String& operator=(const String& h);
};
/****************************/
String::String()
{
	s = NULL;
}

String::String(char *v)
{
	s = v;
}

String::String(String &d)
{
	s = d.s;
}

String::~String(){}

int String::StrLength()
{
	int res = strlen(s);
	return res;
}

void String::StrShow()
{
	std::cout << "stroka: " << s << endl;
}

String& String::operator *(const String& s1)const
{
	int len = strlen(s1.s);
	char* tmp = new char[(len*2)+1];
	tmp = NULL;
	char* t = new char[len+1];
	t = NULL;
	t = s1.s;

	for(int i = 0; i < len; i++)
	{
		tmp[i*2+1] = tmp[i*2] = t[i];
	}
	String newStr(tmp);

	delete tmp;
	return newStr;
}

String& String::operator+(const String& s1)const
{
	int len = strlen(s);
	char* tmp = new char[strlen(s1.s) + strlen(s) + 1];  
	strcpy(tmp, s);
	strcat(tmp, s1.s);

	String newStr(tmp);

	delete tmp;
	return newStr;
}

String& String::operator=(const String& h)
{
	String res;
	res = h.s;
	return res;
}
/**************************/
int _tmain(int argc, _TCHAR* argv[])
{
	String s1 = "abc";
	String s2(s1);
	String s3 = (s1*s1) + s2;

	s1.StrShow();
	cout << "length s1: " << s1.StrLength() << endl;

	//s3.StrShow();
	//cout << "length s3: " << s3.StrLength() << endl;

	return 0;
}

Edited 6 Years Ago by Macbeth: n/a

Error in a loop;

for(int i = 0; i < len; i++)
{
      tmp[i*2+1] = tmp[i*2] = t[i];    // error
}

Your class has a design flaw, it stores a pointer char *s but you point that pointer at either NULL or at data external to the class.

The class should own its own data allocating what it requires with new and deleting it when finished.

This article has been dead for over six months. Start a new discussion instead.