issue with operator=

I just pulled apart my header file, it's giving me weird errors. Does anyone know what the compiler might be complaining about? I'll post it all just in case there's something unrelated.
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
#ifndef ARRAY2D_H_
#define ARRAY2D_H_

#include <new>

class Array2D
{
  public:
	Array2D(); // privates are null
	Array2D(unsigned ht, unsigned wd);
	virtual ~Array2D();
    Array2D(const Array2D& cp);
	Array2D& operator = (const Array2D& cp);
	const char& operator() (unsigned y,unsigned x) const;
	char& operator() (unsigned y,unsigned x);
	// dimensions
	unsigned getWd() const ;
	unsigned getHt() const ;

  private:
	unsigned _height;
	unsigned _width;
	char* _array;
};

#endif /* ARRAY2D_H_ */ 

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
/**






 */

#include "array2d.h"
    
Array2D::Array2D() : _height(0), _width(0), _array(0)
{ }
Array2D::Array2D(unsigned ht, unsigned wd) : _height(ht), _width(wd), _array(0)
{
    if (wd > 0 && ht > 0) _array = new (std::nothrow) char[ht * wd];
}
Array2D::~Array2D()
{
    delete[] _array;
}

Array2D::Array2D(const Array2D& cp)
: _height(cp._height), _width(cp._width),  _array(0)
{
    if (cp._width > 0 && cp._height > 0) _array =
            new (std::nothrow) char[ cp._height * cp._width ];

    for (unsigned i=0; i<(cp._height*cp._width); i++)
        _array[i] = cp._array[i];
}
Array2D::Array2D& operator = (const Array2D& cp)
{
    if(&cp == this)
        return *this;
    _height=cp._height;
    _width=cp._width;

    if (cp._width > 0 && cp._height > 0) _array =
            new (std::nothrow) char[cp._height * cp._width];

    for (unsigned i=0; i<(cp._height*cp._width); i++)
        _array[i] = cp._array[i];

    return *this;
}

const char& Array2D::operator() (unsigned y,unsigned x) const
{
    return _array[ x*_height + y ];
}
char& Array2D::operator() (unsigned y,unsigned x)
{
    return _array[ x*_height + y ];
}


unsigned Array2D::getWd() const { return _width; }
unsigned Array2D::getHt() const { return _height; }


error: 33: 'Array2D& operator=(const Array2D&)' must be nonstatic member function
error: 33: 'Array2D& operator=(const Array2D&)' must take exactly two arguments
error: 34: invalid use of 'this' in non-member function
35 same as above, 
36: _height undeclared (first use this function)
continues for every line on the page
Last edited on
Array2D::Array2D& operator = (const Array2D& cp) <- bad

Array2D& Array2D::operator = (const Array2D& cp) <- good


looks like you pasted the "Array2D::" bit in the wrong spot.
Last edited on
thanks, I'm a bit tired of these cryptic GNU error messages...
Last edited on
they're not that cryptic.

Array2D& operator=(const Array2D&)' must be nonstatic member function


Implies the function is not a member function (which it wasn't, because the scope operator was in the wrong spot)

Either way it should've been a tipoff that something was wrong with your = operator definition. Closer inspection could find the error.
Oh yeah I'm sure, just after 1 week non stop debugging I think all the lines start to blurrrr together :P
I think I really need to learn how to use an IDE debug properly.
Even Gdb I get confused and that has a minimal set of commands(well minimal set on the man page), it rarely tells me anything. If I say "run args1 args2" or w/e. it just runs the program and if it comes to a grinding halt using the 'step' functions simply blurt out random cygwin.dll errors and stackdumps.

As for those above errors, I was focusing on
'must be a nonstatic member function'

and
'must take exactly two arguments'

made me think I had the composition of the function wrong, but not in the way it actually was.

edit: On another topic with the char array declared as char* _array = new char[row*col]; would this automatically be a cstring with a ending '\0' ? or is dynamic a little different to char* pointer.
I've noticed 1 or 2 discrepancies with output, so I thought this might possibly be the case.
as I'm declaring size say 4,4 and completely filling it with chars.
Last edited on
gcampton wrote:
On another topic with the char array declared as char* _array = new char[row*col]; would this automatically be a cstring with a ending '\0' ? or is dynamic a little different to char* pointer.


Neither.

char arrays are no different than any other type of array. The compiler will not put in any null charaters or anything like that, and the value of '0' has no special significance as long as you don't use any cstring functions.

I've noticed 1 or 2 discrepancies with output


? such as?
http://i49.tinypic.com/ztzw3b.jpg

For some reason it only happens on the line of the input file, I have tried deleting that part and re-writing it. It's just really strange, and it doesn't seem to happen with other files.

edit:
I just deleted that puzzle and now the puzzle above it has the same problem, however deleting that puzzle the error disappeared.
Last edited on
Topic archived. No new replies allowed.