Link to home
Start Free TrialLog in
Avatar of chima
chima

asked on

Using append with cin.getline

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;
}
ASKER CERTIFIED SOLUTION
Avatar of Chase707
Chase707

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chima
chima

ASKER

Chase707, thanks...I'll try it tomorrow and post the grade/score tomorrow as well.
Avatar of chima

ASKER

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?
you need to include:

#include <vector>

also, notice the declaration in the example I gave:
   
vector<string> vecStringBuf;
The documentation would be for the vector<> container std class.
Avatar of chima

ASKER

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!).
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.
Avatar of chima

ASKER

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)
Please post the code from line 22 so I can take a look at what is causing the error.

Chase707
Avatar of chima

ASKER

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)
Avatar of chima

ASKER

Bumped points.
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)

Avatar of Mayank S
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.
Avatar of chima

ASKER

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.
Avatar of chima

ASKER

A++, once again thanks