LearnFrenzy
Sign in Take a free test

std::string · Find the Error · Question 9

Error: push_back on a const std::string Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <string>

int main() {
    const std::string base = "file";
    base.push_back('1');
    std::cout << base << '\n';
    return 0;
}
  • A push_back takes a string
  • B base is const, so non-const members such as push_back cannot be called; make a non-const copy Correct
  • C '1' must be "1"
  • D const strings cannot be printed
Correct Answer: Option B

std::string name = base + '1'; builds the new text without modifying the constant.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links