Compiler error do to too much "constness" in my C++ program?

In summary, I get a compiler error that says "in instantiation of member function 'std::__1::pair<const std::__1::basic_string<char>, std::__1::basic_string<char> >::eek:perator=' requested here". I also get a compiler error that says "candidate function not viable: 'this' argument has type 'const std::__1::basic_string<char>', but method is not marked const".
  • #1
Jamin2112
986
12
Can someone try compiler this and tell me what error you get and what it means? https://github.com/jamkin/CSS-Template-Generator/tree/master/CSS%20Template%20Generator

I get
Mod note: Removed the [ I ] tags and the [ color ] BBCode tags, and replaced with [ code ] tags. There were too many emoticons to be able to read the error messages. The error messages are now readable, but some characters seem to have been removed.
Code:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/map:614:15: In instantiation of member function 'std::__1::pair<const std::__1::basic_string<char>, std::__1::basic_string<char> >::eek:perator=' requested here
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__tree:1223:35: In instantiation of member function 'std::__1::__value_type<const std::__1::basic_string<char>, std::__1::basic_string<char> >::eek:perator=' requested here
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/map:941:21: In instantiation of function template specialization 'std::__1::__tree<std::__1::__value_type<const std::__1::basic_string<char>, std::__1::basic_string<char> >, std::__1::__map_value_compare<const std::__1::basic_string<char>, std::__1::__value_type<const std::__1::basic_string<char>, std::__1::basic_string<char> >, std::__1::less<const std::__1::basic_string<char> >, true>, std::__1::allocator<std::__1::__value_type<const std::__1::basic_string<char>, std::__1::basic_string<char> > > >::__assign_unique<const std::__1::pair<const std::__1::basic_string<char>, std::__1::basic_string<char> > *>' requested here
/Users/me/Documents/CSS Template Generator/CSS Template Generator/ProgramDriver.cpp:35:20: In instantiation of member function 'std::__1::map<const std::__1::basic_string<char>, std::__1::basic_string<char>, std::__1::less<const std::__1::basic_string<char> >, std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>, std::__1::basic_string<char> > > >::eek:perator=' requested here
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:1330:19: Candidate function not viable: 'this' argument has type 'const std::__1::basic_string<char>', but method is not marked const
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:1333:19: Candidate function not viable: 'this' argument has type 'const std::__1::basic_string<char>', but method is not marked const
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:1337:45: Candidate function not viable: 'this' argument has type 'const std::__1::basic_string<char>', but method is not marked const
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:1338:19: Candidate function not viable: 'this' argument has type 'const std::__1::basic_string<char>', but method is not marked const
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:1341:19: Candidate function not viable: 'this' argument has type 'const std::__1::basic_string<char>', but method is not marked const
and it's pointing to the following chunk in the <utility> file.

Code:
   _LIBCPP_INLINE_VISIBILITY

    pair&

    operator=(pair&& __p) _NOEXCEPT_(is_nothrow_move_assignable<first_type>::value &&

                                    is_nothrow_move_assignable<second_type>::value)

    {

        first = _VSTD::forward<first_type>(__p.first); // <-------------- This line

        second = _VSTD::forward<second_type>(__p.second);

        return *this;

    }

I asked on Stack Overflow and someone said I was using too much const in my program.
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
You are violating the Rule of Three. You have defined a destructor for many of your classes. The rule of three suggests that you should also be defining a copy constructor and copy assignment operator.

An even bigger problem, at least with regard to compilation, is that you are defining a default constructor for many of your classes. Once you do this, the compiler cannot and must not define a copy constructor or copy assignment operator for you. You must write your own copy constructor and/or copy assignment operator if you want to have a custom default constructor and if you want make copies. You are making copies via your use of iterators, so your code fails.
 
  • #3
It looks like the C++ compiler is recursing through the operator definition with no end in sight and so fails compilation after getting too deep.

Kind of like defining factorial as:

Code:
int factorial(int n) {
    if (n<1) return 1;
    return n*factorial(n);      // NOTICE: should be factorial(n-1) not factorial(n)
}

Running this code you'd get: factorial(5) -- > 5*5*5*...*factorial(5)...

So check your operator definition and see if that maybe you are doing the same kind of thing ie defining it in terms of itself.
 
  • #4
D H said:
You are violating the Rule of Three. You have defined a destructor for many of your classes. The rule of three suggests that you should also be defining a copy constructor and copy assignment operator.

An even bigger problem, at least with regard to compilation, is that you are defining a default constructor for many of your classes. Once you do this, the compiler cannot and must not define a copy constructor or copy assignment operator for you. You must write your own copy constructor and/or copy assignment operator if you want to have a custom default constructor and if you want make copies. You are making copies via your use of iterators, so your code fails.

Thank you for your answer. Since 2 of my 3 destructors are trivial, would not defining them reduce the problems at hand?
 
  • #5
Upon looking at your code in more detail, this is what I see:
  • It's not C++98/03-complaint. You are using (trying to use) braced initialization, something C++98/03 doesn't do all that well. I thought you wanted to learn C++98/03 first!
  • Your code is inconsistent. You are using a set of strings in one place, a vector of strings in another. That mixing and matching will never work.
  • You have too many default constructors and destructors that would best be left undefined (C++98/03) or declared as default (C++11).
  • As noted in the opening post, you are putting too much const into your code.
The last item could perhaps be viewed as a bug in the language. There's no way to change the key in a map or a set. The language makes sure you can't. (A user of a map or set could destroy the integrity of the container if this was allowed. So it isn't allowed.) There's no reason to make the key const because it essentially is const whether or not you declare it as such. Even though the distinction doesn't matter, the language unfortunately does distinguish between std :: set <std :: string > and std :: set < const std :: string >. This is a distinction that shouldn't matter, but it does. This is part of what is getting you in trouble.
 
  • #6
The absence of brace initialization is the 1 thing I know C++11 has that I think C++98 should have (since it has brace initialization for structs, and since it's an extraneous amount of code to initialize vecs/sets/etc. otherwise).

The reason I have a set of strings is because that's an instance in which I don't need any repeated elements.

I must've totally misunderstood the "spirit" of C++, because I thought the idea was to always use const when a variable is intended to never change. At least from the book I read (C++ Primer Plus, 6th Edition) I got the impression that whenever you need a variable, you ask yourself

  • Is this variable ever going to change?
  • Is its address ever going to change?
  • If it's a parameter to a function, do I want to bring in the actual variable or a copy of it?
  • Etc.
I've heard Bjarne Stroustrup say we do all this fancy stuff to "help the compiler."

Anyways, if I have time today then I'll go back and rid my code of the constness and follow the Rule of 3.
 

1. What is a "compiler error due to too much constness" in a C++ program?

A "compiler error due to too much constness" in a C++ program occurs when the use of the const keyword is excessive and causes conflicts or errors in the program's compilation process. This can happen when const is used in places where it is not necessary or when it is used in a way that conflicts with other parts of the code.

2. How does excessive use of const affect my C++ program?

Excessive use of const can lead to compiler errors, which can prevent your program from successfully compiling and running. It can also make your code more difficult to understand and maintain, as the excessive use of const can make it harder to modify or update your code in the future.

3. How can I avoid compiler errors caused by too much constness?

To avoid compiler errors due to too much const in your C++ program, it is important to use const only when necessary. This means using it to declare variables or parameters that should not be modified, but avoiding using it in places where it is not needed. It is also important to ensure that your use of const does not conflict with other parts of your code.

4. Are there any benefits to using const in my C++ program?

Yes, there are benefits to using const in your C++ program. It can help prevent accidental modifications of variables and ensure that they retain their values throughout the program. It can also improve the performance of your code by allowing the compiler to make certain optimizations. However, it is important to use const judiciously to avoid excessive use and potential compiler errors.

5. How can I fix a compiler error caused by too much constness in my C++ program?

To fix a compiler error due to too much const in your C++ program, you will need to identify where the excessive use of const is causing conflicts or errors. This may involve removing unnecessary const declarations, modifying the use of const to avoid conflicts, or rethinking your program's design. It is important to carefully review your code and make targeted changes to address the specific compiler error.

Similar threads

  • Programming and Computer Science
Replies
5
Views
805
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
16
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
17
Views
7K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top