I've been having a hard time understanding classes but i've got it mostly down(atleast I think) well below in the code I overloaded the + operator for my class called clr which uses strings, my problem is on lines 59 and 61(strcpy_s(tmp,len,other.buffer);)

when I strcpy to tmp the word is copied but is then overwritten and whichever word is copied last with strcpy is the one that is returned. I tried fixing it but I am frustrated and dont know what else to do, I feel like something else might be wrong. I added couts at different points in my futile attempt to pin-point the problem but I am as stuck as a glued piece of paper.

#include <iostream>
#include <cstring>

class clr
{
private:
	size_t length;
	char* buffer;
public:
	clr(const char* p = nullptr);
	clr(const clr& other);
	clr& operator=(const clr& copy);
	clr operator+(const clr& copy);
	void print();
	~clr();
};

clr::clr(const char* p) : length(0),buffer(nullptr)
{
	if(p != nullptr)
	{
		length = strlen(p);
		if(length>0)
		{
		buffer = new char[length+1];
		strcpy_s(buffer, length+1,p);
		}
	}	


}

clr::clr(const clr& other)
{
	length = other.length;
	buffer = new char[length+1];
	strcpy_s(buffer,length+1,other.buffer);

}

clr& clr::operator=(const clr& copy)
{
	if((&copy) != this)		
	{

	length = copy.length;
	delete[] buffer;
	buffer = new char[length+1];
	strcpy_s(buffer,length+1,copy.buffer);
	}
	return *this;
}

clr clr::operator+(const clr& other)
{
	size_t len = (this->length + other.length)+1;	
	char* tmp = new char[len];	
	std::cout<<strlen(tmp)<<"\n";
	strcpy_s(tmp,len,other.buffer);
	std::cout<<tmp<<"\n";	
	strcpy_s(tmp,len,buffer);
	std::cout<<tmp;
	std::cout<<"\n .buffer "<<other.buffer;
	std::cout<<"\n buffer "<<buffer;
	
	return clr(tmp);
}

clr::~clr()
{
	delete [] buffer;
}
void clr::print()
{
	std::cout<<"\n"<<buffer<<"\n";
}

int main()
{
	std::cout<<"String Class\n";	
	clr one = "one ";
	clr two = "two";
	clr three = (one + two);
	std::cout<<"\n\n";
	three.print();
	system("pause");
	return 0;

}

You want a function like strcat instead of strcpy in that case (the M$ "safe" equivalent is http://msdn.microsoft.com/en-us/library/d45bbxx4(v=vs.80).aspx strcat_s.
strcpy by nature just clobbers whatever is there.

thanks im gonna go and change it to strcat_s

as soon as I used strcat_s I got an error that a string is not null terminated, probably because strcat_s doesnt not null terminate concatenated chars, now im stuck again. :(

****scratch this post I was using something else which caused the error to popup, i apolagize.***

Please post your current code with the changes.

If I try to use strcat_s it says there is no instance of overloaded function, now I tried just strcat and it concatenated the words but also picks up a lot of junk here is my output as best as I can type it:
==============zzzz1/21/21/2/1/21/21/21/23|twoone

#include <iostream>
#include <cstring>
#include <string>

class clr
{
private:
	size_t length;
	char* buffer;
public:
	clr(const char* p = nullptr);
	clr(const clr& other);
	clr& operator=(const clr& copy);
	clr operator+(const clr& copy);
	void print();
	~clr();
};

clr::clr(const char* p) : length(0),buffer(nullptr)
{
	if(p != nullptr)
	{
		length = strlen(p);
		if(length>0)
		{
		buffer = new char[length+1];
		strcpy_s(buffer, length+1,p);		
		}
	}	


}

clr::clr(const clr& other)
{
	length = other.length;
	buffer = new char[length+1];
	strcpy_s(buffer,length+1,other.buffer);

}

clr& clr::operator=(const clr& copy)
{
	if((&copy) != this)		
	{

	length = copy.length;
	delete[] buffer;
	buffer = new char[length+1];
	strcpy_s(buffer,length+1,copy.buffer);
	}
	return *this;
}

clr clr::operator+(const clr& other)
{
	size_t len = (static_cast<size_t>(this->length + other.length)+1);	
	
	char* tmp = new char[len];	
	
	strcat(tmp,other.buffer);	
	
	strcat(tmp,buffer);	
	
	return clr(tmp);
}

clr::~clr()
{
	delete [] buffer;
}

void clr::print()
{
	std::cout<<buffer;
}

int main()
{
	std::cout<<"String Class\n";	
	clr one = "one ";
	clr two = "two";
	clr three = (one + two);
	std::cout<<"\n\n";
	three.print();
	system("pause");
	return 0;

}

On line 61 do a strcpy instead to move it into the buffer. Right now you're concatenating a good string onto junk. Then concat onto the newly copied string.

It looks like strcat_s takes a size parameter like strcpy_s does, so did you use it with 3 arguments?

Also, since you're going to run into it anyway, look at the order in which you are adding the strings in 61 and 63. So, you'll want to strcpy the first one and strcat the second one.

commented: thanks +1

That did it thanks jonsca owe you another one :),
copy then concatenate, what was I thinking copy then copy , and
yes I am using the 3 argument strcpy and now the strcat

like this

strcpy_s(tmp,len,buffer);	
	
strcat_s(tmp,len,other.buffer);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.