Error C2662, confusion on what is really being done wrong

Don't know why I keep getting e
rror C2662: String1030:: getSize : cannot convert "this' pointer from const string1030 to string 1030&


FILE I CAN CHANGE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  
#include<iostream>
#include "String1030.h"


String1030::String1030(const char *buf):mysize(0)
{	
	//setString(0);
}

String1030::String1030(const String1030& oldstring): mysize(0)
{ 
	
	if(oldstring.getSize()<=0)
	{
		setSize(0);
	}
	else
	{
		setSize(oldstring.getSize());
		for(int i = 0; i < (mysize+1); i++)
		{
			buffer[i] = oldstring.buffer[i];	//.buffer[i];
		}
	}

  
}


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


String1030& String1030::operator=(const String1030& right)
{

	if((&right == this))	//change made
	{
		if(right.getSize()<=0)
		{
			setSize(0);
		}
		else
		{
			//setSize(getSize());
			for(int i = 0; i < (mysize+1); i++)
			{
				buffer[i] = right.buffer[i];
			}
		}

	}


	return *this;
}

char& String1030::operator[](int index)
{
	if(!(index < 0 || index >= mysize)) //recheck
	{
		return buffer[index];
	}
	else return buffer[0]; 
}

int String1030::getSize(void)
{
	return mysize;
}

void String1030::setSize(int newsize)
{
	if(!(newsize < 0)) 
	{
		if(&buffer != NULL) //reeck	
		{
			mysize = newsize;
			//buffer = new char[mysize+1];
		}
		/*else
		{
			mysize = newsize;
			buffer = new char[mysize+1];
		}*/


	}
}

const char* String1030::getString()
{
	return buffer;
}


void String1030::setString(const char *carray)
{
	int index(0);
	if(*carray != 0)
	{
		/*while(carray[index]!= '\0') 
		{
			index++;
		}
		mysize = index;*/
		buffer = new char[mysize+1];
		for(int i = 0; i < (mysize+1); i++)
		{
			buffer[i] = carray[i];
		}
	}
}




Files I am given:
HEADER:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69




#ifndef STRING1030_H
#define STRING1030_H

#include<iostream>

using std::ostream;
using std::istream;
using std::endl;
using std::cerr;


/*
 * The class String1030 is for the students to practice implementing
 * more class functions and making sure that their implementation
 * is reasonable.
 *
 * It requires some skill in using arrays and overloading operators.
 *
 * Note that the sentinel value that MUST be part of the storage for our
 * strings is '\0'.  That is not special, it is just a way to tell future
 * readers that we know what we are doing. We could just as well use the 
 * digit 0, but that can be confusing.
 */

/*
 * If you do not understand any of this, come see me or the TA.
 */

class String1030
{

  public:
      // The constructor. The "0" is the digit 0 NOT a 
      // character. It is used to let us know that 
      // nothing is being passed to the constructor.
    String1030(const char *buf=0);
      //This next is a "copy" constructor. Remember that
      //we have to create new storage and then copy
      //the array content. We must not just copy the pointer.
    String1030(const String1030& oldstring);
      // The destructor because we are allocating memory.
      // We must deallocate it when the object goes out of
      // scope (is destroyed).
    ~String1030();

    String1030& operator=(const String1030& right);

      // Allows to change the element at a certain index.
    char& operator[](int index);

    int getSize(void);
    void setSize(int newsize);
    const char *getString();
      // Replace the existing string with a new one. 
    void setString(const char *carray);


  private:
    char *buffer;
    int mysize;

};


#endif 


OTHER CPP FILE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82


#include<iostream>
#include "String1030.h"

using std::cout;
using std::cin;

int main()
{
  // check the constructors 
  String1030 s("My string");
  String1030 t(s);
  String1030 x;

  char in_buf[256];


  cout << "S size(): " << s.getSize() << endl;
  cout << "T size(): " << t.getSize() << endl;
  cout << "X size(): " << x.getSize() << endl;

  for(int i=0;i<t.getSize();i++)
    cout << t[i];
  cout << endl;

  s[2]='5';

  for(int i=0;i<s.getSize();i++)
    cout << s[i];
  cout << endl;

  // check the assignment operator
  x=s;
  cout << "X: " << x.getString() << endl;

  // check the size reset.
  x.setSize(30);

  cin >> in_buf;
  x.setString(in_buf);
  cout <<  "\nx: " << x.getString() << endl;


  //more checks on resize

  //set to a negative value, nothing should change
  s.setSize(-8);
  cout << "S size(): " << s.getSize() << endl;

  //set to 0, should be 0
  s.setSize(0);
  cout << "S size(): " << s.getSize() << endl;

  //read into the 0 length array should NOT have an error
  //and should NOT transfer any characters. Output should not
  //have any errors either.
  cin >> in_buf;
  s.setString(in_buf);
  cout << "S after cin>>: " << s.getString() << endl;

  //reset to something larger than 0
  s.setSize(10);
  cout << "S size(): " << s.getSize() << endl;

  //read should work now
  cin >> in_buf;
  s.setString(in_buf);
  cout << "S after cin>>: " << s.getString() << endl;

  //now the assignment return value

  x=t=s;

  cout << "T: " << t.getString() << endl;
  cout << "X: " << x.getString() << endl;


  return 0;

}
What line, what file?
Lines 14, 20, 42, all in the first file, on the top. I am using visual studios 2012

Also intellisense says for the same lines(14,20,42) IntelliSense: the object has type qualifiers that are not compatible with the member function
object type is: const String1030 \\wi\Desktop\Prog10\Prog10\String1030.cpp 41 6 Prog10
Last edited on
its because oldstring is declared as const and getSize is not.
line 55 of your header file int getSize(void); change to int getSize(void) const; and do the same in your .cpp file.

I know your not supposed to change the .h file but I don't see a way around it


[EDIT] actually you don't need to use the getSize function to access mysize. you can just use oldstring.mysize;
Last edited on
4 IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type



Error 1 error C2064: term does not evaluate to a function taking 0 arguments


is what i get when i changed the oldstring.getSize to oldstring.mysize. I am confused if your also telling me to remove the getSize below where it returns mysize?
The issue i am having now is the redefinition of default parameter: parameter 1 for the
line 9
String1030::String1030(const char *buf=0) : mysize ()

how do i change this without changing the header's private field to get this to working?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137


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



String1030::String1030(const char *buf=0) : mysize ()
{	
	
	buf = new char;
	*buffer = mysize;
	setString(buf);
	
	
}


String1030::String1030(const String1030& oldstring) :mysize ()
{
	buffer = new char;
    *buffer = *oldstring.buffer;
	
	if(oldstring.mysize <= 0)
	{
		setSize(0);
	}
	else
	{
		setSize(oldstring.mysize);
		for(int i = 0; i < (mysize+1); i++)
		{
			buffer[i] = oldstring.buffer[i];
		}
	}
}


//Reallocates the memory.
String1030::~String1030()
{
	delete [] buffer;
}


//Overloads the = operator, causing one string to be set to another.
String1030& String1030::operator=(const String1030& right)
{
	buffer = new char;
   *buffer = *right.buffer;
	if(!(&right == this))
	{
		if(right.mysize <= 0)
		{
			setSize(0);
		}
		else
		{
			setSize(right.mysize);
			for(int i = 0; i < (mysize+1); i++)
			{
				buffer[i] = right.buffer[i];
			}
		}
	}


	return *this;
}


//Deals with operator overloading of the [] operator.
char& String1030::operator[](int index)
{
	if(!(index < 0 || index >= mysize))
	{
		return buffer[index];
	}
	else return buffer[0]; 
}


//Simple accessor function for size.
int String1030::getSize(void)
{
	int mysize;
	return mysize;
}


//Resetting size of a string.  
void String1030::setSize(int newsize)
{
	if(!(newsize < 0)) 
	{
		if(buffer != NULL)
		{
			mysize = newsize;
			buffer = new char[mysize+1];
		}
		else
		{
			mysize = newsize;
			buffer = new char[mysize+1];
		}


	}
}


//A simple accessor function to return the string.
const char* String1030::getString()
{
	return buffer;
}


//Handles setting a new string.
void String1030::setString(const char *carray)
{
	int index(0);
	if(carray != 0)
	{
		while(*(carray + index) != '\0') 
		{
			index++;
		}
		mysize = index;
		buffer = new char[mysize+1];
		for(int i = 0; i < (mysize+1); i++)
		{
			buffer[i] = carray[i];
		}
	}
}
Last edited on
Topic archived. No new replies allowed.