I seemed to be having an overloading operator issue. I can't really make out what the error is trying to say. Here's the error:
Error: no match for 'operator<<' in
'std::cout << s1.Set::operator+(((const Set&)((const Set*)(& s2))))'
Here's my code:
#include "Set.cpp"
int main(int argc, char *argv[]){
Set s1(10),s2(6),s3(3),s4;
cout<<"First set ({x,y,z}): ";
cin>>s1;
cout<<"A: "<<s1<<endl;
cout<<"Second set: ";
cin>>s2;
cout<<"B: "<<s2<<endl;
cout<<s1+s2<<endl;
}
class Set {
private:
bool elements[255];
int capacity; //Yes, an unsigned char, short, or even size_t, would be better
Set(const bool elements[255], int capacity); //Helpful for immutable types
public:
Set();
Set(short capacity);
friend std::ostream& operator<<(std::ostream &out, Set &set);
friend std::istream& operator>>(std::istream &in, Set &set);
int getCapacity() const; //Cardinality of universe. i.e. |Universe| (or just 'capacity')
};
Set::Set(const bool elements[255], int capacity){
this->capacity = capacity;
for(int i=0; i<255;i++){
if(elements[i] == true && i <= capacity){
this->elements[i] = true;
}
else{
this->elements[i] = false;
}
}
}
Set::Set(short capacity){
this->capacity = capacity;
}
std::ostream& operator<<(std::ostream &out, Set &set) {
int capacity = set.getCapacity();
out<<"{";
for(int i=0; i < 255; i++){
if(set.elements[i] == true ){
out<<i<<",";
}
}
out<<"}";
return out;
}
std::istream& operator>>(std::istream &in, Set &set) {
bool arr[255];
int cap=set.getCapacity();
char open;
in>>open;
if (in.fail() || open!='{') {
in.setstate(std::ios::failbit);
return in;
}
for (int i=0;i<cap;i++)
arr[i]=false;
std::string buff;
std::getline(in,buff,'}');
std::stringstream ss(buff);
std::string field;
while (true) {
std::getline(ss,field,',');
if (ss.fail()) break;
int el;
std::stringstream se(field);
se>>el;
if (el>=0&&el<cap){
arr[el]=true;
}
else{
arr[el]=false;
}
}
set=Set(arr,cap);
}
Set Set::operator+(const Set &other) const{
bool arr[255];
for(int i=0; i<255;i++){
if(this->elements[i] == true || other.elements[i]==true)
arr[i] == true;
}
int capacity = this->capacity>=other.capacity?this->capacity:other.capacity;
return Set(arr,capacity);
}
I overload both the + and >> operators. When executing the code, would it not execute the overloaded + operator first then the >> operator.
Just need some clarification. Thanks
Here's the signature of your overloaded stream insertion operator:
friend std::ostream& operator<<(std::ostream &out, Set &set);
Notice that you're taking your last parameter in as a non-const reference. This means that this function can only take in lvalues as its second parameter. This is fine for all the cases you've listed except this one:
cout << s1+s2 << endl;
I don't believe you included the signature of your operator+ function in your code above, but I'd suspect that it (properly) returns a Set by value. This code gets translated into
(operator<< (cout, s1 + s2)) << endl;
which triggers the issue, since the expression s1 + s2 doesn't evaluate to an lvalue.
To fix this, change the signature of your operator<< function t
friend std::ostream& operator<<(std::ostream &out, const Set &set);
The added const here lets the last parameter bind to anything, including temporaries, which lets you print out s1 + s2 safely. Plus, it (correctly) indicates to the caller that the act of printing out a Set won't actually change that set.
As an aside, it's very weird to include a .cpp file at the top of another .cpp file. You should probably define a header for your Set type and include that. Otherwise, if multiple files try to include the Set.cpp file, you'll get linker errors due to multiple definitions of each of the functions.
Related
Need help figuring out what's the issue with the following source code. I've got a single class to count a number of characters and display each one of them into the console screen.
But unfortunately i kept getting this error message when i tried compiling
error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
The following are the source files:
CharacterCounter.h
#include <iostream>
class CharacterCounter {
public:
int fTotalNumberOfCharacters;
int fCharacterCounts[256]; // hold all 256 byte values
public:
CharacterCounter();
void count(unsigned char aCharacter);
friend std::ostream& operator<<(std::ostream& aOStream, CharacterCounter& aCharacterCounter);
};
}
CharacterCounter.cpp
#include "CharacterCounter.h"
#include <iostream>
/// Constructor
CharacterCounter::CharacterCounter() {
fTotalNumberOfCharacters = 0;
for (int i=0; i < 256; i++) {
fCharacterCounts[i] = i;
}
}
/// Counts the corresponding data member
void CharacterCounter::count(unsigned char aCharacter) {
fTotalNumberOfCharacters++;
fCharacterCounts[aCharacter]++;
}
/// Output stream displays characters greater than 0
std::ostream& operator<<(std::ostream& aOStream, const CharacterCounter& aCharacterCounter) {
for (int i=0; i < 256; i++) {
if (aCharacterCounter.fCharacterCounts[i] > 0) {
int character = aCharacterCounter.fCharacterCounts[i];
aOStream << (unsigned char)i << ":\t" << character << "\n";
}
}
return aOStream;
}
Main.cpp
#include <iostream>
#include <string>
#include "CharacterCounter.h"
using namespace std;
int main() {
CharacterCounter counter;
unsigned char character;
while (cin >> counter) { **Compilation error focuses here**
counter.count(character);
}
cout << counter;
system("pause");
return 0;
};
You need to overload operator >> for your CharacterCounter, because of cin >> counter, declare it:
class CharacterCounter {
public:
//...
friend std::istream& operator>>(std::istream& input, CharacterCounter& aCharacterCounter);
//...
And then declare function overload:
std::istream &operator>>( std::istream &input, CharacterCounter& aCharacterCounter )
{
input >> aCharacterCounter. fTotalNumberOfCharacters;
// Or input something else or more
return input;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a problem and I can't solve it.
note: I can't use any string/strlen/strcmp and such functions
My problem is when I'm trying to print obj after cin, when I don't put him value it prints garbage, and if I put value it change it by cin it print it well.
#ifndef __STRING_H__
#define __STRING_H__
#include <iostream>
using namespace std;
class String {
private:
char* Str; //pointer to string
int Str_s; // size of string public:
String(char* str = NULL);
~String();
void const print() const; //check
//~~~~~~~~~~~~~~~~~~
// operators
//~~~~~~~~~~~~~~~~~
String& operator=(const String& str);
bool operator==(const String& s) const;
bool operator!=(const String& s) const;
void operator-=(const char c);
void operator+=(const char c);
char& operator[](const int index) const;
//~~~~~~~~~~~~~~~~~~
// i/o ~~ operators
//~~~~~~~~~~~~~~~~~
friend ostream& operator<<(ostream&, const String& s);
friend istream& operator>>(istream&, String& s);
//~~~~~~~~~~~~~~~~~~
// arrange method
//~~~~~~~~~~~~~~~~~
void Letters(); // arrange all letter in sentence
void Space(); //remove all unwanted spaces
void Set_Str_s(int num) {
this->Str_s = num;
}
int Get_Str_s() {
return this->Str_s;
}
//~~~~~~~~~~~~~~~~~~
// methods
//~~~~~~~~~~~~~~~~~
char const* STR() {
return Str;
}
};
#endif
ostream& operator<<(ostream& output, const String& s) {
//char *c = s.Str;
//output << c << endl;
for (int i = 0; i < s.Str_s; i++) {
cout << s.Str[i];
}
return output;
}
istream& operator>>(istream& input, String& s) {
if (s.Str == NULL) {
s.Str_s = 0;
char temp[BUFFER];
int i = 0, j = 0;
cin.getline(temp, BUFFER);
while (temp[i] != NULL) {
s.Str_s++;
i++;
}
//s.Str = new char[s.Str_s];
s.Str = temp;
} else {
input >> s.Str;
}
return input;
}
#include <iostream>
using namespace std;
#include "string_head.h"
#include "Definition_head.h"
#include "Menu_head.h"
int main() {
char* c = "hi";
char* h = "lilo";
String s, m(c);
cin >> s;
cin >> m;
cout << s;
cout << endl;
cout << m;
}
The following line is a problem:
s.Str = temp;
you are storing a pointer to a local array that will be deleted when the function returns. Use something like:
s.Str = strdup(temp);
If your platform doesn't support strdup, you can implement it. It's not too hard.
The following line is also a problem:
input >> s.Str;
If there isn't enough memory to hold the string being read, you will be writing into memory that you are not supposed to.
The Problem: When I attempt to assign an IntArray object by index I get the following error:
"Expression is not assignable."
The error is produced by the following line of code in iadrv.cpp:
IntArray a(10);
for(int i = a.low(); i <= a.high(); i++)
a[i] = i * 10;
I am able to assign an entire IntArray object to another like so, a = b;, however when a specific index is referred to the "expression is not assignable" error occurs.
EDIT: I removed the const declaration from most of the functions and I am not getting the "Expression is not assignable" error anymore. However, the setName now gives the error:
"ISO C++ 11 does not allow conversion from string literal to 'char *'"
This error is cause by the following code in iadrv.cpp:
a.setName("a");
Program Explanation:
I have written a class IntArray (in C++) in which the following operators are overloaded:
"[ ]" : allows index range checking
"=" : allows array assignment
"+" : allows the sum of two arrays to be assigned to a third array
"+=" : allows the sum of two arrays to be assigned to the first array
"<<" : allows the contents of an array to be output
The program also includes functions:
setName : sets the name of the IntArray object
getName : returns the name of the IntArray object
low : returns the smallest legal index
high : returns the largest legal index
length : returns the number of elements
A driver program (iadrv.cpp, iadrv.h) will run tests on the IntArray class (IntArray.cpp, IntArray.h) to determine if all operators were properly overloaded.
Note: that for each array test data, the driver will simply multiply the
array index by 10 immediately after each array is initialized or modified and output its contents. When the program encounters a run-time error, it should "simulate"a halt with appropriate diagnostics rather than actually halting the program.
The Code:
IntArray.h
// IntArray.h
#ifndef __IntArray__IntArray__
#define __IntArray__IntArray__
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class IntArray {
private:
int a, b;
int size;
int * num;
char * name;
public:
IntArray(int start, int finish);
IntArray(int finish = 10);
IntArray(const IntArray &); //constructor copy
~IntArray();
int low() const;
int high() const;
char * getName() const;
//removed the const declaration from functions below
int & operator [] (int); //made to return int&
friend ostream & operator << (ostream &, IntArray &);
void setName(char *);
int length() const;
const IntArray & operator = (IntArray &);
const IntArray & operator + (IntArray &);
bool operator += (IntArray &);
};
#endif /* defined(__IntArray__IntArray__) */
IntArray.cpp
// IntArray.cpp
#include "IntArray.h"
#include <iostream>
#include <fstream>
using namespace std;
extern ofstream csis;
IntArray::IntArray(int start, int finish) {
if (start > finish) {
cout << "Simulating a halt.";
a = finish;
b = start;
}
else {
a = start;
b = finish;
}
size = b-a;
num = new int[size];
name = new char[1];
for (int i = 0; i < size; i++) {
num[i] = 0;
}
}
IntArray::IntArray(int finish) {
size = finish;
a = 0;
b = finish - 1;
num = new int[size];
name = new char[1];
for (int i = 0; i < size; i++) {
num[i] = 0;
}
}
IntArray::IntArray (const IntArray & right): size(right.size) {
num = new int[size];
name = new char[1];
for (int i = 0; i < size; i++) {
num[i] = right.num[i];
}
}
IntArray::~IntArray() {
delete[] num;
delete [] name;
}
int IntArray::low() const{
return a;
}
int IntArray::high() const{
return b;
}
char * IntArray::getName() const{
return name;
}
void IntArray::setName(char * n) {
name = n;
}
//removed const declarations
//made to return int&
int & IntArray::operator [] (int subscript) const{
if (subscript < a || subscript > b) {
cout << "subscript: " << subscript << endl;
cout << "Out of bound error. Simulating a halt." << endl;
return num[a];
}
return num[subscript-a];
}
int IntArray::length() const{
//b-a = size
return (b-a);
}
//removed const declarations
ostream & operator << (ostream & output, IntArray & array) {
for (int i = array.low(); i <= array.high(); i++) {
output << array.name << "[" << i << "] = " << array[i] << endl;
}
return output;
}
//removed const declarations
IntArray & IntArray::operator = (IntArray & right) {
if (length() == right.length()) {
for (int i = 0; i <= length(); i++) {
num[i] = right[right.low()+i];
}
return * this;
}
else {
delete [] num; //reclaim space
delete [] name;
size = right.length();
num = new int [size]; //space created
cout << "Different sized arrays. Simulating a hault" << endl;
}
return * this;
}
//removed const declarations
IntArray & IntArray::operator + (IntArray & right) {
int * ptr;
ptr = new int [right.length()];
if (length() == right.length()) {
for (int i = 0; i < length(); i++) {
ptr[i] = num[i] + right[right.low()+i];
}
}
return * this;
}
//removed const declarations
bool IntArray::operator += (IntArray & right) {
if (length() == right.length()) {
for (int i = 0; i <= right.length(); i++) {
num[i] += right[right.low()+i];
}
return true;
}
cout << "Could not add the sum of the arrays into first array. Simulating a halt." << endl;
return false;
}
iadrv.h
// iadrv.h
#ifndef p6_iadrv_h
#define p6_iadrv_h
#include "intarray.h"
int main();
void test1();
void wait();
#endif
iadrv.cpp
// iadrv.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include "iadrv.h"
using namespace std;
ofstream csis;
int main() {
csis.open("csis.dat");
test1();
csis.close();
}
void test1() {
system("clear");
cout << "1. Array declared with single integer: IntArray a(10);" << endl << endl;
csis << "1. Array declared with single integer: IntArray a(10);" << endl << endl;
IntArray a(10);
for(int i = a.low(); i <= a.high(); i++)
a[i] = i * 10;
a.setName("a");
cout << a << endl;
csis << a << endl;
wait();
}
DISCLAIMER: This program was written as a school assignment and has already been turned in to be graded. This was my first c++ program so I would like to understand my mistakes. Your help is sincerely appreciated.
You have defined your operator[] like this:
const int operator [] (int) const;
that second "const" means that inside that method you cannot modify your object.
So it will only work for getting values, but not for setting them.
Try to remove it and it should work.
EDIT: AS pointed to Bryan Chen, You also need to return a reference and non-const, like this:
int& operator [] (int subscript)
Now, looking more in depth at your code, that is not even enough, because you have this method:
ostream & operator << (ostream & output, const IntArray & array) {
for (int i = array.low(); i <= array.high(); i++) {
output << array.name << "[" << i << "] = " << array[i] << endl;
}
return output;
}
Look that you operator[] needs to work on a non-const IntArray, but in that method your variable "array" is const, so you need to rewrite a bit more of code.
Also, look for the same problem with the rest of the operators, remember: you make a method 'const' only if you don't plan to modify the object from inside that method, and you make a parameter 'const' only if you don't plan to modify that parameter.
Your existing operator does not allow the value to be changed, both because it returns an int by value and because the operator is declared const. You can't assign to a value, only to an object (which includes references, since a reference is just another name for an object).
To accomplish this you need to supplement your existing operator with another, non-const one that returns a reference to the (non-const) int:
int & operator[](int index);
Since this operator would return a reference, you can assign directly to the return value using the familiar a[b] = c syntax you desire to use.
You would not need to change your existing operator, but I would strongly recommend changing the return type from const int to just int -- you are returning by value anyway, so you are handing back a copy. It makes no sense for this to be const, and this may prevent the compiler from eliding copies in the case of more complex data types than int. (It doesn't really make much difference here, but I would avoid getting in the habit of returning both by value and const, since -- assuming the presence of a copy constructor -- the const qualifier can be removed anyway by simply copying the value again. Returning const copies usually provides no benefits while having several drawbacks.)
Since you also asked to point out your mistakes, I would like to comment on two things you should/could have done to make the code more simple:
First, the assignment operator could have been written like this:
IntArray& operator=(IntArray rhs)
{
std::swap(rhs.a, a);
std::swap(rhs.b, b);
std::swap(rhs.size, size);
std::swap(rhs.num, num);
std::swap(rhs.name, name);
return *this;
}
This works, since you have a copy constructor and destructor already defined for IntArray, and they hopefully work correctly. All the assignment operator is doing is creating a temporary object and swapping out its internals with the internals of the current object. Then the temporary dies with the "old data", while the new data is safely in the current object. This is called the copy/swap idiom.
Also note that a reference is returned that is non-const.
If you pass a const reference instead of an object, then the assignment operator is responsible for the initial copy made.
IntArray& operator=(const IntArray& orig)
{
IntArray rhs(orig);
std::swap(rhs.a, a);
std::swap(rhs.b, b);
std::swap(rhs.size, size);
std::swap(rhs.num, num);
std::swap(rhs.name, name);
return *this;
}
The former version may be faster, due to allowing the compiler to optimize the copy of the passed value. However the second form of passing a const reference is what is usually done -- note that the temporary object needs to be created inside the function before proceeding.
Second, your operator + can just use operator +=:
IntArray operator+(const IntArray& rhs)
{
IntArray temp(*this);
return temp += rhs;
}
All we did was create a temporary equal to the current object. Then we use += to add rhs and return the result. Nice and simple. Note that operator + returns a new IntArray object, not a const IntArray. In addition, operator += should return a reference to the current object, not bool.
To take advantage of this, your operator += should be rewritten thusly:
IntArray& operator+=(const IntArray& rhs)
{
//..your current code goes here:
//...
return *this;
}
Also, your operator += should not be "erroring out" like that. You need to make the class more robust by attempting to add two IntArrays that may not be the same size. If there really is an error throw an exception. Don't return a bool -- remove the return true; and return false; from the function altogether. Always return *this.
struct myVals {
int val1;
int val2;
};
I have static functions
static myVals GetMyVals(void)
{
// Do some calcaulation.
myVals val;
val.val1 = < calculatoin done in previous value is assigned here>;
val.val2 = < calculatoin done in previous value is assigned here>;
return val;
}
bool static GetStringFromMyVals( const myVals& val, char* pBuffer, int sizeOfBuffer, int count)
{
// Do some calcuation.
char cVal[25];
// use some calucations and logic to convert val to string and store to cVal;
strncpy(pBuffer, cVal, count);
return true;
}
My requirement here is that i should have above two functions to be called in order and print the string of "myvals" using C++ output operator (<<).
How can we achieve this? Does i require new class to wrap this up. Any inputs are help ful. Thanks
pseudocode:
operator << () { // operator << is not declared completely
char abc[30];
myvals var1 = GetMyVald();
GetStringFromMyVals(var1, abc, 30, 30);
// print the string here.
}
The signature for this operator is as follows:
std::ostream & operator<<(std::ostream & stream, const myVals & item);
An implementation could look like this:
std::ostream & operator<<(std::ostream & stream, const myVals & item) {
stream << item.val1 << " - " << item.val2;
return stream;
}
I'm having the above issue when I attempt to set a string (stored in a class) equal to another string. I have combed and combed trying to find if I did not initialize any variables, but I can't find such a situation. In debug mod, I get the above error. In release mode it hangs and Win7 looks for a problem, no major abort or retry window. Here's the relevant code, there is another header file with my main program if you feel it should be included, I'll include the line that causes errors. Language is C++ obviously.
//Error occurs in this area:
Car one;
one = two;
one.addExtra ("Windows");
log << "Car one: " << one << endl;
two = Car(one); // call copy constructor.
//I realize when I call the first one = two, there are no extras
//stored int Car one, which is what differs between the two. Remaining
//code. Extras header:
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
class Extras
{
public:
friend class Car;
friend int main();
friend ostream& operator << (ostream& os, const Extras& in);
friend class CarLot;
Extras(const Extras& other);
Extras& operator=(Extras &rhs);
Extras(string in);
Extras();
~Extras();
void modify_ext(string* in);
//string ex_list;
private:
int place;
string *ex_list;
};
//Extras.cpp:
#include "Extras.h"
Extras::Extras(string in)
{
delete ex_list;
ex_list = new string;
place = 0;
//ex_list = new string[4];
(*ex_list) = in;
place++;
}
Extras::Extras()
{
//ex_list = new string[4];
place = 0;
//for(int i = 0; i < 4; i++)
ex_list = new string;
*ex_list = "0";
}
//Overloaded << operator for Extras class to
//easily output array contents
ostream& operator<< (ostream& os, Extras const &in)
{
os << *(in.ex_list);
return os;
}
Extras& Extras::operator=(Extras &rhs)
{
if(this != &rhs)
{
//string temp;
//temp = rhs.ex_list;
modify_ext(rhs.ex_list);
cout << endl << endl << ex_list << endl << endl;
place = rhs.place;
}
return *this;
}
Extras::Extras(const Extras& other) : place(other.place), ex_list(other.ex_list)
{
//for(int i = 0; i < 4; i++)
//ex_list = other.ex_list;
}
void Extras::modify_ext(string* in)
{
delete ex_list;
ex_list = new string;
(*ex_list).resize((*in).size());
for(unsigned int i = 0; i < (*in).size(); i++)
ex_list[i] = in[i];
}
Extras::~Extras()
{
delete ex_list;
place = 0;
}
//Car Header:
#include "Extras.h"
class Car
{
public:
friend class Extras;
friend Extras& Extras::operator=(Extras &rhs);
friend int main();
friend ostream& operator<< (ostream& os, const Car& in);
friend class CarLot;
friend void add_extra();
~Car();
Car();
Car(Car& other);
Car(string in_name, int in_year, string in_color, float in_cost);
Car& operator=(Car const &rhs);
void edit_extr(int in);
void addExtra(string in);
private:
string name, color;
int year, extr_num;
float cost;
Extras *extr;
};
//Car.cpp:
#include "car.h"
//Constructor
Car::Car(string in_name, int in_year, string in_color, float in_cost)
{
name = in_name;
color = in_color;
year = in_year;
cost = in_cost;
extr = new Extras[3];
extr_num = 0;
}
//Overloaded = operator
Car& Car::operator=(Car const &rhs)
{
if(this != &rhs)
{
name = rhs.name;
color = rhs.color;
year = rhs.year;
cost = rhs.cost;
//delete extr;
extr = rhs.extr;
extr_num = rhs.extr_num;
}
return *this;
}
//Default Constructor
Car::Car()
{
name = "TEMP";
color = "BLUE";
year = 0;
cost = 0;
extr = new Extras[3];
extr_num = 0;
}
//Destructor
Car::~Car()
{
delete extr;
extr = NULL;
}
//Copy constructor
Car::Car(Car& other) : name(other.name), color(other.color), year(other.year),
cost(other.cost), extr_num(other.extr_num)
{
//delete extr;
for(int i = 0; i < extr_num; i++)
{
extr[i].modify_ext(other.extr[i].ex_list);
extr[i].place = other.extr[i].place;
}
}
//Overloaded << operator for Car class
ostream& operator<< (ostream& os, const Car& in)
{
os.precision(2);
os << in.name << ", " << in.year << ", "
<< in.color << ", $"<< in.cost << ", ";
os << "extras include: ";
for(int k = 0; k < in.extr_num; k++)
{
os << in.extr[k] << ", ";
}
os << endl;
return os;
}
void Car::edit_extr(int in)
{
Extras* temp;
temp = new Extras[in];
for(int i = 0; i < in; i++)
temp[i] = extr[i];
extr_num = in;
delete extr;
extr = temp;
}
void Car::addExtra(string in)
{
if(extr_num == 3)
{
//log << "Car has too many extras.";
return;
}
//edit_extr(extr_num + 1);
*(extr[extr_num].ex_list) = in;
extr[extr_num].place++;
extr_num++;
}
As I said, I have one more additional header, another class, and a main program if those need to be included, but I figured this was more than enough code (sorry!) for anyone to look through. Any help would be incredibly appreciated.
What I'm seeing that's broken:
two = Car(one); // call copy constructor.
No, it creates a temporary object with the copy constructor, passes this to operator=() on two, then destroys the temporary.
Extras& operator=(Extras &rhs);
should be:
Extras& operator=(const Extras &rhs);
Extras::Extras(string in)
{
delete ex_list;
ex_list = new string;
place = 0;
//ex_list = new string[4];
(*ex_list) = in;
place++;
}
Better:
Extras::Extras(const string& in): place(1), ex_list(new string(in))
{
}
Extras::Extras(const Extras& other) : place(other.place), ex_list(other.ex_list)
{
//for(int i = 0; i < 4; i++)
//ex_list = other.ex_list;
}
Looking at your default constructor, it's clear that the Extras object owns the string in ex_list. However, this copy constructor claims ownership of the original object's ex_list. It should make its own copy:
Extras::Extras(const Extras& other): place(other.place),
ex_list(new string(other.ex_list))
{
}
void Extras::modify_ext(string* in)
{
delete ex_list;
ex_list = new string;
(*ex_list).resize((*in).size());
for(unsigned int i = 0; i < (*in).size(); i++)
ex_list[i] = in[i];
}
You're copying the string. All you need is:
void Extras::modify_ext(const string* in)
{
*ex_list = *in;
}
Moving on to the Car...
friend class Extras;
friend Extras& Extras::operator=(Extras &rhs);
friend int main();
friend ostream& operator<< (ostream& os, const Car& in);
friend class CarLot;
friend void add_extra();
You should consider refactoring the code to get rid of these.
Car(Car& other);
Car(string in_name, int in_year, string in_color, float in_cost);
Should be:
Car(const Car& other);
Car(const string& in_name, int in_year, const string& in_color, float in_cost);
References are your friends when passing objects to functions.
I'm going to stop here.
In your constructor you are deleting ex_list. It hasn't even been allocated yet, so this is wrong. Remove that and do this instead when allocating your new string:
ex_list = new string(in);
This way you use the string copy constructor. You can get rid of the rest of whatever you were trying to do below in the constructor since this will do it for you.
Edit:
Actually, there are a ton of problems all throughout this code. Is there any reason you want your string to be a pointer internally? You aren't using pointers correctly in a bunch of different places. I only noticed the first one as I scrolled down.
The allocator in debug builds with VC++ uses some magic values to fill allocated areas. In particular, 0xCC is memory that had been allocated but it now freed. So the address 0xCCCCCCD0, looks suspiciously like a small offset (e.g., to a struct or class member) from a pointer in memory that was freed. Given that, Mark Loeser's answer looks promising.