I'm getting following compile error

OrderedList.cc: In member function `std::string OrderedList::find(std::string&, std::string&, int&)':
OrderedList.cc:133: error: invalid conversion from `int' to `const char*'
OrderedList.cc:133: error: initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'

OrderedList.cc looks like this

string OrderedList::find(string& w, string& d, int& s)
{
  keyValue* p = head;
  if(p != NULL)
    {
      w = p->word;
      d = p->definition;
      head = p->next;

      if(head == NULL)
	{
	  s = 0;
	  return(w, d, s);
	}
      else
	{
	  return(w, d, 1);
	}
    }//end p !=NULL
 
 else
    {
      return(w, d, 0);
    }
}//end find

and its called by following function

void dynamicHTable::sorted()
{
  string word = NULL;
  string definition = NULL;
  int status = 0;

  for(int i = 0; i < TABLE_SIZE-1; i++)
    {
      dHTable[i].find( word, definition, status);
      if (status ==1)
	{
	  do{
	    Table.insert(word, definition);
	    dHTable[i].find( word, definition, status);
	  }while(status ==1);
	}//if ebd
    }//for end
}//sorted end

Recommended Answers

All 5 Replies

You can only return a single item using a return statement. In the function definition you indicate that find will return a string. In the return statement you appear to be trying to return two strings and an int. It may work, but I doubt it. The compiler appears to be complaining that you are trying to return an int when it is expecting a string and it can't find a conversion from int to string.

In addition, w, d, and s are passed by reference so any changes to their value is automatically reflected in the calling function so you don't need to return the values in the return statement?

You can only return a single item using a return statement.

Lol, who told you this. You can return as many values as you wish. Only have to configure recive function to store more than one value

I did small change to this find function so looks like this

string OrderedList::find(string& w, string& d, string& s)
{
  keyValue* p = head;
  if(p != NULL)
    {
      w = p->word;
      d = p->definition;
      head = p->next;

      if(head == NULL)
	{
	  s = 'n';
	  return(w, d, s);
	}
      else
	{
               s = 'y';
	  return(w, d, s);
	}
    }//end p !=NULL
 
 else
    {
      s = 'n';
      return(w, d, s);
    }
}//end find

and updated function sorted

void dynamicHTable::sorted()
{
  string word;
  string definition;
  string status;

  for(int i = 0; i < TABLE_SIZE-1; i++)
    {
      dHTable[i].find( word, definition, status);
      if (status == "y")
	{
	  do{
	    Table.insert(word, definition);
	    dHTable[i].find( word, definition, status);
	  }while(status == "y");
	}//if ebd
    }//for end
}//sorted end

No more error messages. Well if somebody know smarter implementation let me know :cheesy:

>> You can return as many values as you wish.

admittedly, I don't own a copy of the standard, but at least one "reasonably" respected author, J Liberty in "Teaching yourself C++ in 24 days", and the only "author" I have available to rely on besides prior experience, repeatedly indicates that functions can only return a single value using the a return statement. I have also encountered this concept from multiple other discussions when describing why passing by reference is so useful and why you can't return an array and why you might want to bundle multiple return values into an instance of a struct/class and return it, etc.

As I am not a guru or an expert I'd appreciate other input into the "debate".

It appears that the program will compile with multiple variables in the return statement. However, all I got back was the last variable. So, with

return(d, w, s);

only the "s" was returned. I used Visual Studio 6.

Try running this code...it will make it clear

#include <iostream>

int main()
{	
	int number;
	number=(1,2,3);
	std::cout<<number;
        number=1,2,3;
	std::cout<<number;
	
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.