const retval ns::operator[]( args ) const; problem

Hi,

Could someone try explaining why is this happening:

function definition in class:
const string& operator[]( string& key ) const;
implementation:
const string& CHashTable::operator[]( string& key ) const
{
return cht_[ key ];
}

compiler says:
opt.cpp:10: error: passing 'const CHashTable' as 'this' argument of 'const std::string& CHashTable::operator[](std::string&)' discards qualifiers

Why is it loosing "const"?
This turns out to be some kind of mess with my makefile.
I have two ways to build a project: splitted cpp files, which make objects and all headers and cpp-s packed into one header file to be used.
If I pack them, it works.

I experiment with my makefile, so it is very well possible, there is something off. But not necessarily.
However, I doubt if anyone has time to read this...

makefile:
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
SHELL = /bin/bash
.SUFFIXES: .cpp .o

product = argo
CC = g++
CFLAGS = -Wall
names = arg opt HashTable Argo
objdir = objects
objects := $(addsuffix .o,$(names))
fpobjects := $(addprefix $(objdir)/,$(objects))

ifeq ($(PACKED_MAKE),1)
$(shell ./MakeHeader.sh)
$(product): argo.o
    $(CC) $(CFLAGS) argo.o -o $@

%.o: %.cpp %.h
    $(CC) $(CFLAGS) -c $<
else
$(product): $(objects) argo.o
    $(CC) $(CFLAGS) $^ -o $@

argo.o: argo.cpp
    $(CC) $(CFLAGS) -c $<

opt.o: opt.cpp HashTable.cpp HashTable.h opt.h
    $(CC) $(CFLAGS) -c $<

Argo.o: Argo.cpp arg.cpp opt.cpp HashTable.cpp Argo.h arg.h opt.h HashTable.h
    $(CC) $(CFLAGS) -c $<

%.o: %.cpp %.h
    $(CC) $(CFLAGS) -c $<
endif

.PHONY: clean
clean:
    -rm -f $(product) $(fpobjects) $(objects) argo.o

What's type is cht_? Does is have a const index operator that takes a string?
I think, it doesn't matters about cht_, if it can be compiled well in one way of *compiling*, but not another.
Don't pay attention. It is about makefiles as I see.
Topic archived. No new replies allowed.