Cannot convert from `std::string*' to non-scalar type `std::string

conversion from `std::string*' to non-scalar type `std::string

Page 1 of 1

4 Replies - 3443 Views - Last Post: 27 August 2010 - 05:13 AM Rate Topic: -----

#1 Guest_James*


Reputation:

Cannot convert from `std::string*' to non-scalar type `std::string

Posted 27 August 2010 - 02:52 AM

Hello, I'm having a lot of trouble with the following error message: conversion from `std::string*' to non-scalar type `std::string' requested ", on line 54 of my code.

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
#include "queues.h"
using namespace std;
const int strSize=80;
string* calc(string o1, string o2, int op);
int check(string op);
string intToString(int x);

int main ()
{
	//	Local Definitions
	string* dataPtr;
	QUEUE* input;
	QUEUE* output;
	std::string* dataPtr1;
	std::string* dataPtr2;
	std::string* dataPtr3;
	std::string* dataPtr4;

	//	Statements
	// Create a queue and allocate memory for data
	input = createQueue ();
	output = createQueue ();

	// Fill queue
	while(!cin.eof())
	{
	    dataPtr = new string[strSize];
        cout << "Enter a number: <EOF> to stop: ";
        cin >> *dataPtr;
        enqueue (input, dataPtr);
	}
	
	//move to output
	while(!emptyQueue(input))
	{
		int temp;
		dataPtr1 = (string*)dequeue (input);			// remove first token
		temp=check(dataPtr1[0]);
		if(temp>0) 
		{
			dataPtr2 = (string*)queueFront(input);				// peek 2nd token
			if(isdigit(atoi(dataPtr2[0].c_str()))) 
			{
				dataPtr2 = (string*)dequeue(input);				// remove 2nd token
				dataPtr3 = (string*)queueFront(input);		// peek 3rd token
			
				if(isdigit(atoi(dataPtr3[0].c_str())))
				{
					dataPtr3 = (string*)dequeue(input);				// remove 3rd token
					dataPtr4 = calc(dataPtr2, dataPtr3, temp);  // perform calc
					enqueue(output,dataPtr4);					// put result in output Q
				}			
				else 
				{	
					enqueue(output,dataPtr1);		// didn't do calc put tokens in output 
					enqueue(output,dataPtr2);
				}
			}		 
			else 
			{
				enqueue(output,dataPtr1);			// didn't do calc put token in output
			}
		} 
		else if (isdigit(atoi(dataPtr1[0].c_str()))) 
			enqueue(output,dataPtr1);			// operand only put token in output
	}

	//	Now print strings
	cout << "\n\nThe list of strings:\n";
	while (!emptyQueue (output))
	   {
	    dataPtr = (string*)dequeue (output);
	    cout << *dataPtr  << endl;
	    free (dataPtr);
	   } // while

	//	Destroying Queue
	destroyQueue (input);
	system("pause");
	return 0;
}	// main

int check(char op)
{
 	switch(op) 
	{
    	case '+':
		return 1;
     	case '-':
		return 2;
      	case '*':
		return 3;
       	case '/':
		return 4;
		default: 
		return 0;
	}
}

string* calc(string o1, string o2, int op)
{	// Applies operator to two operands as strings
	// returns a pointer to a string
	// uses atoi() to convert string to integer
	int result;
	string *s;
	s = new string[strSize];
 	switch(op) 
	{
		case 1:
			result=atoi(o1.c_str())+atoi(o2.c_str());
			break;
		case 2:
			result=atoi(o1.c_str())-atoi(o2.c_str());
			break;
      	case 3:
			result=atoi(o1.c_str())*atoi(o2.c_str());
			break;
       	case 4:
			result=atoi(o1.c_str())/atoi(o2.c_str());
			break;
 	}
	s=&intToString(result);
 	return s;
}

string intToString(int x)
{
	string r;
	stringstream s;

	s << x;
	r = s.str();

	return r;
}



I have no idea what's causing it, I've tried googling but i wasn't able to solve it from the few threads I found, so my last resort is to ask the question here before i submit non working code to my lecturer. Thanks in advance.

Is This A Good Question/Topic? 0

Replies To: Cannot convert from `std::string*' to non-scalar type `std::string

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,606
  • Joined: 23-August 08

Re: Cannot convert from `std::string*' to non-scalar type `std::string

Posted 27 August 2010 - 04:22 AM

You've defined calc like this:
string* calc(string o1, string o2, int op);

Note the first two parameters...they're not string *s, but strings, but you're passing string pointers:
std::string* dataPtr2;
std::string* dataPtr3;
...
dataPtr4 = calc(dataPtr2, dataPtr3, temp);

Was This Post Helpful? 0
  • +
  • -

#3 Guest_James*


Reputation:

Re: Cannot convert from `std::string*' to non-scalar type `std::string

Posted 27 August 2010 - 04:57 AM

Thank you for you help. With this code it solves that error problem, however I am now getting "[Linker error] undefined reference to `check(std::string)" on line 126. Any ideas?

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
#include "queues.h"
using namespace std;
const int strSize=80;
string* calc(string *o1, string *o2, int op);
int check(string op);
string intToString(int x);

int main ()
{
    //    Local Definitions
    string* dataPtr;
    QUEUE* input;
    QUEUE* output;
    std::string* dataPtr1;
    std::string* dataPtr2;
    std::string* dataPtr3;
    std::string* dataPtr4;

    //    Statements
    // Create a queue and allocate memory for data
    input = createQueue ();
    output = createQueue ();

    // Fill queue
    while(!cin.eof())
    {
        dataPtr = new string[strSize];
        cout << "Enter a number: <EOF> to stop: ";
        cin >> *dataPtr;
        enqueue (input, dataPtr);
    }
    
    //move to output
    while(!emptyQueue(input))
    {
        int temp;
        dataPtr1 = (string*)dequeue (input);            // remove first token
        temp=check(dataPtr1[0]);
        if(temp>0) 
        {
            dataPtr2 = (string*)queueFront(input);                // peek 2nd token
            if(isdigit(atoi(dataPtr2[0].c_str()))) 
            {
                dataPtr2 = (string*)dequeue(input);                // remove 2nd token
                dataPtr3 = (string*)queueFront(input);        // peek 3rd token
            
                if(isdigit(atoi(dataPtr3[0].c_str())))
                {
                    dataPtr3 = (string*)dequeue(input);                // remove 3rd token
                    dataPtr4 = calc(dataPtr2, dataPtr3, temp);  // perform calc
                    enqueue(output,dataPtr4);                    // put result in output Q
                }            
                else 
                {    
                    enqueue(output,dataPtr1);        // didn't do calc put tokens in output 
                    enqueue(output,dataPtr2);
                }
            }         
            else 
            {
                enqueue(output,dataPtr1);            // didn't do calc put token in output
            }
        } 
        else if (isdigit(atoi(dataPtr1[0].c_str()))) 
            enqueue(output,dataPtr1);            // operand only put token in output
    }

    //    Now print strings
    cout << "\n\nThe list of strings:\n";
    while (!emptyQueue (output))
       {
        dataPtr = (string*)dequeue (output);
        cout << *dataPtr  << endl;
        free (dataPtr);
       } // while

    //    Destroying Queue
    destroyQueue (input);
    system("pause");
    return 0;
}    // main

int check(char op)
{
     switch(op) 
    {
        case '+':
        return 1;
         case '-':
        return 2;
          case '*':
        return 3;
           case '/':
        return 4;
        default: 
        return 0;
    }
}

string* calc(string *o1, string *o2, int op)
{    // Applies operator to two operands as strings
    // returns a pointer to a string
    // uses atoi() to convert string to integer
    int result;
    string *s;
    s = new string[strSize];
     switch(op) 
    {
        case 1:
            result=atoi(o1->c_str())+atoi(o2->c_str());
            break;
        case 2:
            result=atoi(o1->c_str())-atoi(o2->c_str());
            break;
          case 3:
            result=atoi(o1->c_str())*atoi(o2->c_str());
            break;
           case 4:
            result=atoi(o1->c_str())/atoi(o2->c_str());
            break;
     }
    s=&intToString(result);
     return s;
}

string intToString(int x)
{
    string r;
    stringstream s;

    s << x;
    r = s.str();

    return r;
}


Was This Post Helpful? 0

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Cannot convert from `std::string*' to non-scalar type `std::string

Posted 27 August 2010 - 05:00 AM

int check(string op); You say there's a function named check, which takes a string as an argument. Where do you define that function?
Was This Post Helpful? 1
  • +
  • -

#5 Guest_James*


Reputation:

Re: Cannot convert from `std::string*' to non-scalar type `std::string

Posted 27 August 2010 - 05:13 AM

THANK YOU!!! I fixed the prototype and it now compiles, now I just have to get it working properly. :)
Was This Post Helpful? 0

Page 1 of 1