0

I need to take some code within a function and put that within yet another function. The only problem is the variables are now out a scope. Whenever I try to pass them both as references I can hit with an onslaught of errors.

The relevant part of my code looks something like this:

Route::Route(std::string source) //constructor function
{
    std::ostringstream oss;

    function(source, oss);
}

void function(std::string* &source, std::ostringstream* &oss)
{
    //function
}

The constructor should do things with source and oss, and then the function should also do things with them. Is it purely a syntactical error, or am I trying to do something impossible?

2 Answers2

3

That function doesn't take references to the objects, but to pointers. It should be

void function(std::string &source, std::ostringstream &oss)

although source should probably be passed by value (std::string) or constant reference (const std::string &), unless the function is supposed to modify it.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

It is a pure syntactic error, what you want to do is fine:

Route::Route(std::string source) //constructor function
{
    std::ostringstream oss;

    function(source, oss);
}

void function(std::string& source, std::ostringstream &oss)
{
    //function
}

you only need to get rid of the * which would mean in your case (std::string*&) reference to pointer of string.

I guess you may have mistaken those syntaxes with the & operator of C that gives the address of a variable to be given to a foo* type that gets an address.

zmo
  • 24,463
  • 4
  • 54
  • 90