Passing Strings by Reference

This is a discussion on Passing Strings by Reference within the C++ Programming forums, part of the General Programming Boards category; I'm having some trouble passing strings by reference. Below is some code: Code: int guessConsonant(string,string&); //function prototype ... .. . ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    Passing Strings by Reference

    I'm having some trouble passing strings by reference.

    Below is some code:
    Code:
    int guessConsonant(string,string&); //function prototype
    ...
    ..
    .
    //function call (mysteryWord is declared as a normal string)
    TotalSoFar = guessConsonant(rWord, &mysteryWord); 
    ...
    ..
    ..
    //function header
    int guessConsonant(string currentWord, string &mWord)
    So yeah, I get these error when I try to use the "guessConsonant" function:

    Code:
    wof2.cpp: In function `int main()':
    wof2.cpp:49: conversion from `string *' to non-scalar type `basic_string<char,string_char_traits<char>,__default_alloc_template<true,0> >' requested
    wof2.cpp:8: in passing argument 2 of `guessConsonant(basic_string<char,string_char_traits<char>,__default_alloc_template<true,0> >, string &)'
    wof2.cpp: In function `int guessConsonant(basic_string<char,string_char_traits<char>,__default_alloc_template<true,0> >, string &)':
    wof2.cpp:105: no match for `*basic_string<char,string_char_traits<char>,__default_alloc_template<true,0> > &'
    Basically, all I want to be able to do is pass a string to a function, manipulate the string inside the function, and have the changes affect the original string I passed to the function(passing by reference, right?) and for some reason my compiler won't accept what I'm giving it. So any help would be vastly appreciated.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    You don't need to prefix the variable with the ampersand symbol (&) when you pass by value or by reference.
    Code:
    TotalSoFar = guessConsonant(rWord, mysteryWord);
    You will need an ampersand in the following case
    Code:
    int guessConsonant(string, string *); //function prototype
    ...
    TotalSoFar = guessConsonant(rWord, &mysteryWord);
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    Question

    Now I get an error when I call the method:

    Code:
    wof2.cpp:47: warning: cannot pass objects of type `string' through `...'
    
    wof2.cpp:47: warning: cannot pass objects of type `string' through `...'
    I don't use the ampersand when I call the method and pass a string variable.

    I have no Idea what that error message means.

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Can you post the whole error/warning message?
    Also post the offending lines of code
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing structure by reference or pointer?
    By 7force in forum C Programming
    Replies: 8
    Last Post: 12-13-2010, 06:49 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  4. Question about OpenGL/Linux
    By Ideswa in forum Linux Programming
    Replies: 12
    Last Post: 09-10-2006, 06:56 AM
  5. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 05:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21