error C2440: '=' : cannot convert from 'std::string' to 'char *'
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: error C2440: '=' : cannot convert from 'std::string' to 'char *'

  1. #1
    Join Date
    Jan 2006
    Posts
    326

    error C2440: '=' : cannot convert from 'std::string' to 'char *'

    I am going to form arrays through string. How to convert from std:string to char.

    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    #include<sstream>
     
    	using namespace std;
    int main()
    {
    char *str1[5], *str2[5], *test_string[5];
    int  *input, i;
    ifstream in("testdfa.txt");  //open input file
    string inbuf;
    string old_state, symbol, new_state;
     i = 0 ;
    while (getline(in, inbuf))  {
    	istringstream is (inbuf);
    	is >> old_state;
    	if (old_state != "FINAL") {
    		is >> symbol >> new_state;
    		str1[i] = old_state;
    		str2[i] = &new_state;
    		input[i] = symbol;
    		i += 1;
    	}
    	else
    		{test_string[i] = old_string;
    	i += 1;}
    	}
    	cout<<str1[1];
    }
    c:\documents and settings\owner\my documents\visual studio 2005\projects\dfa\dfa\dfa.cpp(20) : error C2440: '=' : cannot convert from 'std::string' to 'char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    c:\documents and settings\owner\my documents\visual studio 2005\projects\dfa\dfa\dfa.cpp(21) : error C2440: '=' : cannot convert from 'std::string *__w64 ' to 'char *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    c:\documents and settings\owner\my documents\visual studio 2005\projects\dfa\dfa\dfa.cpp(22) : error C2440: '=' : cannot convert from 'std::string' to 'int'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    c:\documents and settings\owner\my documents\visual studio 2005\projects\dfa\dfa\dfa.cpp(26) : error C2065: 'old_string' : undeclared identifier
    Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\dfa\dfa\Debug\BuildLog.htm"
    dfa - 4 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: error C2440: '=' : cannot convert from 'std::string' to 'char *'

    How to convert from std:string to char.
    You can use the std::string.c_str() function:
    Code:
    std::string str = "some string";
    const char* psz = str.c_str();
    But, the const char* returned from c_str is only valid as long a the std::string object is valid. If you want to keep the char* beyond that you should make a copy:
    Code:
    std::string str = "some string";
    char* psz = strdup(str.c_str());
    
    //...
    
    // free string
    free(psz);
    - petter

  3. #3
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,051

    Re: error C2440: '=' : cannot convert from 'std::string' to 'char *'

    [ Moved thread ]

  4. #4
    Join Date
    Oct 2007
    Posts
    1

    Re: error C2440: '=' : cannot convert from 'std::string' to 'char *'

    You dont need to do anything just include the foolowing file
    #include <sstream> at the begining

    Rajavardhan Sarkapally

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: error C2440: '=' : cannot convert from 'std::string' to 'char *'

    why do you want all these char * arrays? Unless of course you want to pass into a function like execv().

    In that case you should use vector<char> instead of string and set up your char* arrays by taking the address of the first element of your vectors. Note that you will need to do a push_back('\0') on each of your vectors.

    I actually wrote a wrapper class called ncstring thus:

    Code:
    class ncstring
    {
    private:
       std::vector< char > m_vec;
    
    public:
       ncstring( const std::string & str )
         : m_vec( str.begin(), str.end() )
       {
         m_vec.push_back( '\0' );
       }
    
       char * get()
       {
           return &m_vec[0];
       }
    
       size_t size() const
       {
           return m_vec.size() - 1;
       }
    };
    I also create a class called argvec which has underlying vector< char * > and effectively builds up what you need for argv/argc arguments. It's fairly straightforward to write and I'll leave it to you, given what you have seen me do above.
    Last edited by NMTop40; October 6th, 2007 at 04:07 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center