# Help! I don't know what I'm doing wrong to get: error C2440: '=' : cannot convert...

This topic is 3316 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm getting errors on my program, and I'm not sure what I am doing wrong, can anyone point me in the right direction? I tried looking up ways to fix it, but since I don't understand the problem, I'm not getting anywhere. Here's my code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
// Declare a string variable for the polynomial and the current term
string poly, curr;

// Declare an array of strings size 10 for the coefficients, and another for the exponents
char coefficient[10], exponents[10];

// Declare integer variables for the position of x, of the caret, of the sign, and for the
// term counter.
int pos_x, pos_caret, pos_sign, term_counter;

// prompt and input a polynomial
cout << "Enter a polynomial" << endl;
cin >> poly;

// initialize the term counter
term_counter = 0;

// while the polynomial is not empty and term counter is less than 10
while (!poly.empty() && term_counter < 10)
{

This is where my errors begin:
		// else the coefficient is that part of the term before the x
else
{
coefficient[term_counter]= curr.substr(0,pos_x);
}

// find the position of the ^
pos_caret = curr.find("^",0);

// if there is a ^ character, the exponent is the string after the ^ character
if (pos_caret!=-1)
{
int len = curr.length();
exponents[term_counter]=curr.substr(pos_caret,len);

} my errors are:
1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>Compiling...
1>etst.cpp
1>c:\users\owner\documents\visual studio 2008\projects\test\test\etst.cpp(87) : error C2440: '=' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\owner\documents\visual studio 2008\projects\test\test\etst.cpp(97) : error C2440: '=' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Build log was saved at "file://c:\Users\Owner\Documents\Visual Studio 2008\Projects\test\test\Debug\BuildLog.htm"
1>test - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Thanks

##### Share on other sites
You're attempting to assign an instance of std::string to char. These types are, of course, not compatible.

It looks like you meant to write:

std::string coefficient[10], exponents[10];

char coefficient[10], exponents[10];

##### Share on other sites
std::string.substr() returns another std::string. Basically you are trying to do this:

std::string s = "Hello World";
char c = s.substr(0, 5); // Error! substr() returns an std::string, not a char

You can't convert an std::string to a char, so the compiler is giving you an error. You need to make coefficient and exponents arrays of std::strings in order for it to work like that.

I need to start keeping a record of how many times I've been ninja'd [rolleyes]

##### Share on other sites
What do you expect the result of assigning a string to a character to be? (Answering this question should help you in identifying the problem.)

[Oops, way too slow...]

##### Share on other sites
The errors are really quite accurate. Lets parse them:
1>c:\users\owner\documents\visual studio 2008\projects\test\test\etst.cpp(87) : >> error C2440: '=' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char'1>        with1>        [1>            _Elem=char,1>            _Traits=std::char_traits<char>,1>            _Ax=std::allocator<char>1>        ]1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

First lets strip some of the stuff - the path is irrelevant and the numbers:
cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char'        with        [            _Elem=char,            _Traits=std::char_traits<char>,            _Ax=std::allocator<char>        ]No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Now substitute the types:
cannot convert from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'char'No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

std::string is really a typedef of std::basic_string<char,std::char_traits<char>,std::allocator<char>>. We can do yet another substitution:
cannot convert from 'std::string' to 'char'No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Let look at the line causing the error:
coefficient[term_counter]= curr.substr(0,pos_x);

The problem is that the compiler sees this:
<char> = <std::string>

Which is obviously wrong.

Lets look further up:
// Declare an array of strings size 10 for the coefficients, and another for the exponentschar coefficient[10], exponents[10];

The comment does not match the declaration. You should change the declaration to:
std::string coefficient[10], exponents[10];

##### Share on other sites

This topic is 3316 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628694
• Total Posts
2984253

• 16
• 13
• 13
• 10
• 10