3
void SetString(char s1[], char s2[]){
    str1= s1[]; 
    str2= s2[];

}
private: 
    char str1[20];
    char str2[20];

What's wrong in that code please some one tell me.

Its giving me following error:

stringhandler.cpp: In member function ‘void StringHandler::SetString(char*, char*)’:

stringhandler.cpp:25: error: expected primary-expression before ‘]’ token stringhandler.cpp:26: error: expected primary-expression before ‘]’ token

enter image description here enter image description here

3
  • 1
    str1= s1[]; That's not valid syntax, what are you trying to do? Commented Oct 6, 2016 at 10:28
  • im just trying to pass two string and assigning it to the class fields Commented Oct 6, 2016 at 10:30
  • 2
    @MaheshDhokade Actually use strings then, that will make your life way easier. Commented Oct 6, 2016 at 10:34

2 Answers 2

4

You can't copy C-strings like that, s1 and s2 both decay to a pointer and you have to use strcpy to copy them:

strcpy(str1, s1);
strcpy(str2, s2);

But you're working with C++ so really you should be using std::string:

void SetString(const std::string& s1, const std::string& s2)
    {
        str1= s1; 
        str2= s2;

    }
private: 
    std::string str1;
    std::string str2;

or, for C++11 and higher:

void SetString(std::string s1, std::string s2)
    {
        str1= std::move(s1); 
        str2= std::move(s2);

    }

This solves a lot of problems that come with C-strings for you.

Sign up to request clarification or add additional context in comments.

5 Comments

s1 and s2 should be passed by const & here or overloaded for lvalue and rvalue references.
Or, str1 = std::move(s1) and so on.
@juanchopanza There's a chance OP doesn't have access to C+11, I didn't want to risk getting a compilation error if that's the case.
OK, then it isn't clear what you gain from passing by value. Maybe you break even for rvalues, but lose out for lvalues.
im just doing in the program for string operation without using string functions for that im accepting strings from user in main and trying to assign to the class field as u can see i have declared the class fields as private so i can't access it in main function so please simply guide me how to assign the user entered string to the private fields
4

You can not do str1=s1[]. It is not valid. Use this instead to copy an array to another:

void SetString(char s1[], char s2[]){
    std::copy(s1, s1+20, std::begin(str1));
    std::copy(s2, s2+20, std::begin(str2));
}

Even better, use std::array instead (if your case is general) or std::string if you are dealing with strings as array of char. Something like this:

void SetString(const std::array<char,20>& s1, const std::array<char,20>& s2){
    str1=s1;
    str2=s2;
}
private: 
    std::array<char,20> str1;
    std::array<char,20> str2;

or:

void SetString(const std::string& s1, const string& s2){
    str1=s1;
    str2=s2;
}
private: 
    std::string str1;
    std::string str2;

2 Comments

Passing through const & is usually pessimistic nowadays. Especially if you're already going to copy them.
@M.M Thanks for notifying

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.