Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6538097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:41:43+00:00 2026-05-25T10:41:43+00:00

My class has a member std::string received; , initialized at an empty string in

  • 0

My class has a member std::string received;, initialized at an empty string in its constructor, along with a function printReceived that prints the string to cout.

In main(), an instance of the above class is created, and printReceived is called.

Instead of getting an empty string, I get totally unexpected values (but always the same):

  • If printReceived is std::cout<<"Received ":<<received<<std::endl;, I get

    Received: eived: as output.

  • A string constant present in a function of another class which is not called, if this file is linked.

Where could that come from ? It’s getting me mad… All variables are correctly initialized. I’ve never had this problem before, and I’ve programmed a lot in C++.

Here is a complete minimal example as asked:

CellularTest.cpp

#include "A.h"

#include <iostream>

int main()
{
    A s;

    s.println("AT+CSQ");
    
    return 0;
}

A.cpp

#include "A.h"

A::A()
: received("")
{
}
void A::println(char* s)
{
    received+=s+'\n';
    treatReceived();
}
void A::treatReceived()
{
    std::cout<<"Received: "<<received<<std::endl;
}

A.h

#include <iostream>
#include <string>

class A
{
    public:
        A();
        void println(char* s);
    private:
        std::string received;
        void treatReceived();
};

Makefile

CellularTest: CellularTest.o CellularTest.cpp A.o
    g++ CellularTest.o A.o -o CellularTest

CellularTest.o: CellularTest.cpp

A.o: A.cpp A.h

clean:
    rm *.o
    rm CellularTest

The output I get is:

Received: eived: 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:41:44+00:00Added an answer on May 25, 2026 at 10:41 am

    operator+= has lower precedence than operator+. So in println, you’re doing this:

    received+=(s+'\n');
    

    Which is like

    received+=(s+10);
    

    Which is incrementing the pointer s by 10 bytes and then appending the string pointed to by the resulting char* to received, and the string literal Recieved: is happening to be stored just after the string literal AT+CSQ. So memory might look like AT+CSQ\0Received: \0 and incrementing AT+CSQ by 10 is actually the e in Received:. So there you have it.

    Change that to

    received+=s;
    received+='\n';
    

    Or alternatively

    received = received + s + '\n';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Message that has a std::string as a data member, defined
The class Item has a member function text() that returns a list of strings.
Let's say I have a class that has a member called data which is
I have a base class that has a private static member: class Base {
I am attempting to make a class that has a template object member inside
I have a class Class which has a member std::list, I want to search
In C++ I have defined a class that has this as a member: static
I have a class myclass that has a private member param_map class some_class {
I've been trying to call the overloaded table::scan_index(std::string, ...) member function without success. For
I want to export a C++ class, which has a member std::vector<int> . How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.