0

I'm not a c++ developer but my current project is updating a legacy c++ application.

All throughout the application I see append statements that take two arguments..

strTemp.append( cDollars, len );

I'm trying to understand the C++ language and how it is implemented in this legacy application.

I'm stuck on the append function.

I can get it to work with a three arguments..

#include <iostream>
#include <string>

using namespace std;

int main(){

   string msg = "Hi There!";
   string msg2;

   msg2.append(msg,0, 2);

   cout << msg2;
   cout << "\n";
   system("pause");
   return 0;
}

This returns...

Hi
Press any key to continue . . .

I can get append to work with a single argument..

#include <iostream>
#include <string>

using namespace std;

int main(){

   string msg = "Hi There!";
   string msg2;

   msg2.append(msg);

   cout << msg2;
   cout << "\n";
   system("pause");
   return 0;
}

This returns...

Hi There!
Press any key to continue . . .

The problem is that I can't get this to work with a two arguments...

#include <iostream>
#include <string>

using namespace std;

int main(){

   string msg = "Hi There!";
   string msg2;

   msg2.append(msg, 1);

   cout << msg2;
   cout << "\n";
   system("pause");
   return 0;
}

--------------------Configuration: Program - Win32 Debug--------------------
Compiling...
Program.cpp
C:\USERS\E1009028\DOCUMENTS\VISUAL STUDIO 6 PROJECTS\SCRATCH\Program\Program.cpp(11) : error C2664: 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &__thiscall std::basic_string<char,struct std::char_traits<c
har>,class std::allocator<char> >::append(const char *,unsigned int)' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

Program.exe - 1 error(s), 0 warning(s)

This is Visual Studio 6 (did I mention the legacy application?).

Can anyone tell me what I'm missing?

Thanks,

codingguy3000
  • 2,695
  • 15
  • 46
  • 74
  • 2
    `append`'s 2 parameter version doesn't take a `string`. It expects a character array. Well, that or a repeat count and a single character or a couple iterators, but those are definitely not what you're after. – user4581301 Nov 29 '17 at 17:41
  • 2
    mst2.append(msg.c_str(), 1) should do the job. – UKMonkey Nov 29 '17 at 17:42
  • I amend previous comment again: `msg2.append(msg.begin(), msg.begin()+1);` would also work so long as you make sure `msg` has at least one character. But it's nasty. – user4581301 Nov 29 '17 at 17:45

0 Answers0