Compiler Error

I'm doing a school assignment and I'm having a small error issue. I have too CPP files and two heads


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
#include "sl_list.h"
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include <sstream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::stringstream;

SLList::SLList() {
  head_ = NULL;
  size_ = 0;
}

SLList::~SLList() {
  delete [] head_;
  size_ = 0;
}

void SLList::InsertHead(int i) {
  SLNode * node;
  node = new SLNode(i);
  node->set_next_node(head_);
  head_ = node;
  size_++;
}

void SLList::RemoveHead() {
  if (size_ != 0) {
    SLNode * node;
    node = head_;
    head_ = head_->next_node();
    delete node;
    size_--;
  }
}

void SLList::Clear() {
  delete head_;
}

unsigned int SLList::size() const {
  return size_;
}

string SLList::ToString() const {
  std::ostringstream ss;
  if (size_ != 0) {
    ss << head_;
    for (int i = 1; i < size_; i++) {
      head_ = head_->next_node();
      ss << ", " << head_;
    }
    return ss.str();
  } else {
    return "";
  }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef SL_LIST_H
#define SL_LIST_H
#include <string>
#include <climits>
#include "sl_node.h"

class SLList {
public:
  SLList();
  ~SLList();
  void InsertHead(int i);
  void RemoveHead();
  void Clear();
  unsigned int size() const;
  std::string ToString() const;
private:
  SLNode * head_;
  unsigned int size_;
};

#endif /* SL_LIST_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
#include "sl_node.h"
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include <sstream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::stringstream;

SLNode::SLNode() {
  next_node_ = NULL;
  contents_ = 0;
}

SLNode::SLNode(int size) {
  next_node_ = NULL;
  contents_ = size;
}

SLNode::~SLNode() {
  delete next_node_;
  contents_ = 0;
}

void SLNode::set_contents(int num) {
  contents_ = num;
}

int SLNode::contents() const {
  return contents_;
}

void SLNode::set_next_node(SLNode* node) {
  next_node_ = node;
}

SLNode* SLNode::next_node() const {
  return next_node_;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef SL_NODE_H
#define SL_NODE_H
#include <string>
#include <climits>
  
class SLNode {
public:
  SLNode();
  SLNode(int size);
  ~SLNode();
  void set_contents(int num);
  int contents() const;
  void set_next_node(SLNode* node);
  SLNode* next_node() const;
private:
  SLNode * next_node_;
  int contents_;
};

#endif /* SL_NODE_H */ 


And this is my compile error:

1
2
3
4
5
$ g++ lab_18.cpp sl_list.cpp
sl_list.cpp: In member function ‘std::string SLList::ToString() const’:
sl_list.cpp:60:13: error: assignment of member ‘SLList::head_’ in read-only object
       head_ = head_->next_node();
             ^
Thankfully, your compiler has given you a meaningful message.

The line
head_ = head_->next_node();
is attempting to modify the state of your SLList object. Through the assignment operator, it will change what head_ contains. But your SLList::ToString() function has been declared as const, and ensures that it won't affect the state of the object. So you can see the conflict?
Last edited on
That error is quite verbose.

The SLList::ToString() const promises that the SLList will not change during the function and the compiler checks that the code of the function does not break that promise.

However, the code of the function attempts to change the this->head_. That is a blatant violation of constness.


Lets assume that you can change the head_ in the function. At the end of the function the SLList will have no clue about where all but the last node of the list are. That is bad, isn't it.


Lets look at the SLList::RemoveHead(). You call delete on the first node. The destructor of that node calls delete on the next node, whose destructor calls delete on the next node, whose ...

After the function the SLList still has non-zero size_ and the head_ points to node that has already been deleted.


In short, there are several details for you to pay attention to.
So how do I move through all the units in the node in ToString then?
Last edited on
Topic archived. No new replies allowed.