?
Solved

Using append with cin.getline

Posted on 2003-03-06
15
Medium Priority
?
391 Views
Last Modified: 2013-12-14
What syntax does it take to make the
pStringBuf[1].append(str1) work.

error C2228: left of '.append' must have class/struct/union type
Error executing cl.exe.

See below for the code.

These statements don't work as well...why not?
// append the first 2 items from an array of the element type
    char achTest[] = {'8', '9', 'A'};
    //str1.append(achTest, 2); //works
     //achTest[0].append(str1,2);//does not work

    //cout << "str1 = " << str1.c_str() << endl;

    // append all of a string literal to str1
    char szTest[] = "ABC";
    //str1.append(szTest); //works
     //szTest.append(str1);//does not work



#include <string>
#include <iostream>

using namespace std ;

int main()
{
     const int stringSize = 10;
     const int numOfPhrases =3;
     char *pStringBuf[numOfPhrases];

     for(int i=0;i<numOfPhrases;i++){
     pStringBuf[i]= new char[stringSize];
     }

     for(i=0;i<numOfPhrases;i++){
     cin.getline(pStringBuf[i],stringSize,'\n');
     }

     cout<< endl << pStringBuf[1] << endl;

    string str1("012");
    string str2("345");

    cout << "str1 = " << str1.c_str() << endl;

    // append str2 to str1
    str1.append(pStringBuf[1]);  //This works

    pStringBuf[1].append(str1); //This does not work.Why not?

    cout << "str1 = " << str1.c_str() << endl;

    return 0;
}
0
Comment
Question by:chima
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
15 Comments
 
LVL 4

Accepted Solution

by:
Chase707 earned 300 total points
ID: 8085694
>>pStringBuf[1].append(str1); //This does not work.Why not?

pStringBuf is an array of char*, not string.

std::string is class from the Standard Template Library that represents string data, i.e. "my string", and inherently provides useful operations on the string data such as append.

char* is essentially just an array of char terminated by the '\0' char -- it is a basic type and does not have any inherent functionality.  There are C library functions to manipulate char* data.

in most cases you can use std::string uniformally in your code.

...

#include <string>
#include <vector>
#include <iostream>

using namespace std ;


int main()
{
    const int stringSize = 10;
    const int numOfPhrases =3;
    vector<string> vecStringBuf;
     char szInput[stringSize];

    for(int i=0; i < numOfPhrases; i++)
    {
     cin.getline(szInput, stringSize, '\n');
     vecStringBuf.push_back(std::string(szInput));
    }

    cout<< endl << vecStringBuf[0] << endl;

   string str1("012");
   string str2("345");

   cout << "str1 = " << str1.c_str() << endl;

   // append str2 to str1
   str1.append(vecStringBuf[0]);  //This works

   vecStringBuf[0].append(str1); //works now

   cout << "str1 = " << str1.c_str() << endl;
   cout << "vecStringBuf[0] = " << vecStringBuf[0] << endl;

   return 0;
}

Chase707
0
 

Author Comment

by:chima
ID: 8085878
Chase707, thanks...I'll try it tomorrow and post the grade/score tomorrow as well.
0
 

Author Comment

by:chima
ID: 8089223
Chase707, I tried your suggestion and I'm getting this error; error C2065: 'vecStringBuf' : undeclared identifier.
Also, I looked in MSDN library, a current C++ Std Library book, and could not fine any information on vecStringBuf.push_back.  Where could I find documentation on this?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:Chase707
ID: 8090306
you need to include:

#include <vector>

also, notice the declaration in the example I gave:
   
vector<string> vecStringBuf;
0
 
LVL 4

Expert Comment

by:Chase707
ID: 8090312
The documentation would be for the vector<> container std class.
0
 

Author Comment

by:chima
ID: 8091542
Chase707, I did not have vector<string> vecStringBuf;  added.  I added it, and now I have error C4786 and it points to the vector header file.  Along with one error I have 6 warnings.  
I might not be able to get to this problem right away, but being that you are the only one to answer I'll work with you (or please work with ME!).
0
 
LVL 4

Expert Comment

by:Chase707
ID: 8091626
please post the complete text of the error message so I can take a look at it.

most likely the warnings are a quirk of the MS debug build, and will go away with a Release build.

But, post the full text of the error.
0
 

Author Comment

by:chima
ID: 8092883
Chare707,per your request:
Compiling...
DynamicString1.cpp
c:\program files\microsoft visual studio\vc98\include\vector(48) : warning C4786: '??0?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QAE@IABV
?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@1@ABV?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@1@@Z' : identifier was truncated to '255' characters in the browser information
        C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \2\DynamicString1.cpp(11) : see reference to class template instantiation 'std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::alloca
tor<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >' being compiled
c:\program files\microsoft visual studio\vc98\include\vector(61) : warning C4786: '??0?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QAE@PBV?
$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@1@0ABV?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@1@@Z' : identifier was truncated to '255' characters in the browser information
        C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \2\DynamicString1.cpp(11) : see reference to class template instantiation 'std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::alloca
tor<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >' being compiled
c:\program files\microsoft visual studio\vc98\include\vector(103) : warning C4786: '?rbegin@?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QA
E?AV?$reverse_iterator@PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@AAV12@PAV12@H@2@XZ' : identifier was truncated to '255' characters in the browser information
        C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \2\DynamicString1.cpp(11) : see reference to class template instantiation 'std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::alloca
tor<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >' being compiled
c:\program files\microsoft visual studio\vc98\include\vector(105) : warning C4786: '?rbegin@?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QB
E?AV?$reverse_iterator@PBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@ABV12@PBV12@H@2@XZ' : identifier was truncated to '255' characters in the browser information
        C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \2\DynamicString1.cpp(11) : see reference to class template instantiation 'std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::alloca
tor<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >' being compiled
c:\program files\microsoft visual studio\vc98\include\vector(107) : warning C4786: '?rend@?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QAE?
AV?$reverse_iterator@PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@AAV12@PAV12@H@2@XZ' : identifier was truncated to '255' characters in the browser information
        C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \2\DynamicString1.cpp(11) : see reference to class template instantiation 'std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::alloca
tor<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >' being compiled
c:\program files\microsoft visual studio\vc98\include\vector(108) : warning C4786: '?rend@?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QBE?
AV?$reverse_iterator@PBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@ABV12@PBV12@H@2@XZ' : identifier was truncated to '255' characters in the browser information
        C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \2\DynamicString1.cpp(11) : see reference to class template instantiation 'std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::alloca
tor<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >' being compiled
C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \2\DynamicString1.cpp(22) : error C2440: 'type cast' : cannot convert from 'char *[3]' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<
char> >'
        No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.

DynamicString1.obj - 1 error(s), 6 warning(s)
0
 
LVL 4

Expert Comment

by:Chase707
ID: 8094410
Please post the code from line 22 so I can take a look at what is causing the error.

Chase707
0
 

Author Comment

by:chima
ID: 8095243
Line 22 is
vecStringBuf.push_back(std::string(pStringBuf));

I corrected the problem and now I get 10 warnings

Found within this loop, correction shown below:

for(i=0;i<numOfPhrases;i++){
      cin.getline(pStringBuf[i],stringSize,'\n');
      vecStringBuf.push_back(std::string(pStringBuf[i]));}

The first warning points to the VECTOR header file:

vector(const _Myt& _X)
: allocator(_X.allocator)
{_First = allocator.allocate(_X.size(), (void *)0);
_Last = _Ucopy(_X.begin(), _X.end(), _First);
_End = _Last; }

This are the warnings:
compiling...
DynamicString1.cpp
c:\program files\microsoft visual studio\vc98\include\vector(48) : warning C4786: '??0?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QAE@IABV
?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@1@ABV?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@1@@Z' : identifier was truncated to '255' characters in the browser information
        C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \DynamicString1.cpp(11) : see reference to class template instantiation 'std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::alloca
tor<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >' being compiled
c:\program files\microsoft visual studio\vc98\include\vector(61) : warning C4786: '??0?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QAE@PBV?
$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@1@0ABV?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@1@@Z' : identifier was truncated to '255' characters in the browser information
        C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \DynamicString1.cpp(11) : see reference to class template instantiation 'std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::alloca
tor<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >' being compiled
c:\program files\microsoft visual studio\vc98\include\vector(103) : warning C4786: '?rbegin@?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QA
E?AV?$reverse_iterator@PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@AAV12@PAV12@H@2@XZ' : identifier was truncated to '255' characters in the browser information
        C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \DynamicString1.cpp(11) : see reference to class template instantiation 'std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::alloca
tor<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >' being compiled
c:\program files\microsoft visual studio\vc98\include\vector(105) : warning C4786: '?rbegin@?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QB
E?AV?$reverse_iterator@PBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@ABV12@PBV12@H@2@XZ' : identifier was truncated to '255' characters in the browser information
        C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \DynamicString1.cpp(11) : see reference to class template instantiation 'std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::alloca
tor<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >' being compiled
c:\program files\microsoft visual studio\vc98\include\vector(107) : warning C4786: '?rend@?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QAE?
AV?$reverse_iterator@PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@AAV12@PAV12@H@2@XZ' : identifier was truncated to '255' characters in the browser information
        C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \DynamicString1.cpp(11) : see reference to class template instantiation 'std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::alloca
tor<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >' being compiled
c:\program files\microsoft visual studio\vc98\include\vector(108) : warning C4786: '?rend@?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QBE?
AV?$reverse_iterator@PBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@ABV12@PBV12@H@2@XZ' : identifier was truncated to '255' characters in the browser information
        C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \DynamicString1.cpp(11) : see reference to class template instantiation 'std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::alloca
tor<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >' being compiled
C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \DynamicString1.cpp(74) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::
char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters i
n the debug information
C:\Program Files\Microsoft Visual Studio\MyProjects\C++ \DynamicString1.cpp(74) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_t
raits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug inform
ation
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
 >::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
 >::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information

DynamicString1.obj - 0 error(s), 10 warning(s)
0
 

Author Comment

by:chima
ID: 8095245
Bumped points.
0
 
LVL 4

Expert Comment

by:Chase707
ID: 8095374
Those warnings are part of combining the MS debug library with the Standard Template Library and can be ignored.  You will not get these warnings in the release build.

If you want to disable the warning, put the following line after your stdafx.h include, if you have one, and before your <string>, <iostream> and other STL includes

#pragma warning(disable:4786)

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8117992
Dear Chima,

Referring to your initial program:

append () is a method of string, and so, it can only be accessed with objects of class string. Since 'pStringBuf' has bene defined as char **, so it won;t be able to access that method ever. However, you can change it to

string pStringBuf[numOfPhrases] ;

Or if you want to continue with your initial code, just write:

strcat ( pStringBuf[1], str1 ) ;

Hope that helps!

Mayank.
0
 

Author Comment

by:chima
ID: 8169810
Chase707, sorry for the absence, had a death in the family.  Just getting back.  Thanks for your comments and help.  Please keep in touch for other questions I may have.

Mayank, thanks for your comments.  The use of char** as described by Chase707 worked very well.  I realize that it was an awkward way of dealing with the string, but I needed to fulfill the requirement to use a function named append, concat, etc., for the program.
0
 

Author Comment

by:chima
ID: 8169815
A++, once again thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question