function to return string array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • askalottaqs
    New Member
    • Jun 2007
    • 75

    function to return string array

    i created a function which u send a string, and it tokenizes according to spaces, so i send it a string for it to return me a string array, but on the declaration of the function it says

    error C2470: 'splitter' : looks like a function definition, but there is no parameter list; skipping apparent body
    and
    error C2092: 'splitter' array element type cannot be function

    and my header is

    string splitter[](string line)

    i read the other day that the return value could be anythin as long as its one object ,int, string, array, as long as its one object, so what am i doing wrong?

    thanks in advance
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by askalottaqs
    i created a function which u send a string, and it tokenizes according to spaces, so i send it a string for it to return me a string array, but on the declaration of the function it says

    error C2470: 'splitter' : looks like a function definition, but there is no parameter list; skipping apparent body
    and
    error C2092: 'splitter' array element type cannot be function

    and my header is

    string splitter[](string line)

    i read the other day that the return value could be anythin as long as its one object ,int, string, array, as long as its one object, so what am i doing wrong?

    thanks in advance
    it's because of spliter[],you cannot have array element type as a return value.Change it to *spliter and it will work.

    Savage

    Comment

    • askalottaqs
      New Member
      • Jun 2007
      • 75

      #3
      thanks a lot :D it worked

      it all worked, function wise, but when i call it, i get this error

      warning C4172: returning address of local variable or temporary

      do u happpen to know why that is?? thanks a lot again

      Comment

      • askalottaqs
        New Member
        • Jun 2007
        • 75

        #4
        well i figured i had to have it declared withthe public functions, which i did, but now i get a new error,

        1>retrieveGeoCm dCmd.obj : error LNK2019: unresolved external symbol "class std::basic_stri ng<char,struct std::char_trait s<char>,class std::allocator< char> > __cdecl splitter(class std::basic_stri ng<char,struct std::char_trait s<char>,class std::allocator< char> >)" (?splitter@@YA? AV?$basic_strin g@DU?$char_trai ts@D@std@@V?$al locator@D@2@@st d@@V12@@Z) referenced in function "public: virtual class MStatus __thiscall retrieveGeoCmd: :doIt(class MArgList const &)" (?doIt@retrieve GeoCmd@@UAE?AVM Status@@ABVMArg List@@@Z)


        yes this is one error :S

        help??

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          Originally posted by askalottaqs
          thanks a lot :D it worked

          it all worked, function wise, but when i call it, i get this error

          warning C4172: returning address of local variable or temporary

          do u happpen to know why that is?? thanks a lot again
          Can I take a look on your spliter function?

          Savage

          Comment

          • askalottaqs
            New Member
            • Jun 2007
            • 75

            #6
            there u go
            [code=cpp]
            string *splitter(strin g line)
            {
            int strBegin=0, strLocale=0, i=1, strCount=0, j, strLngth ;
            string str, thread[100];
            str = line;

            while(strLocale != string::npos)
            {
            strLocale = str.find(" ",strBegin) ;
            strLngth = strLocale-strBegin;
            thread[i] = str.substr(strB egin,strLngth);
            strBegin = strLocale + 1;
            strCount++;
            i++;
            };

            for (j = 1; j <= strCount; j++)
            cout << thread[j] << " ";
            return thread;

            }[/code]
            Last edited by sicarie; Sep 19 '07, 07:44 PM. Reason: Code tags

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #7
              Originally posted by askalottaqs
              there u go

              [CODE="cpp"
              ]string *splitter(strin g line)
              {
              int strBegin=0, strLocale=0, i=1, strCount=0, j, strLngth ;
              string str, thread[100];
              str = line;

              while(strLocale != string::npos)
              {
              strLocale = str.find(" ",strBegin) ;
              strLngth = strLocale-strBegin;
              thread[i] = str.substr(strB egin,strLngth);
              strBegin = strLocale + 1;
              strCount++;
              i++;
              };

              for (j = 1; j <= strCount; j++)
              cout << thread[j] << " ";
              return thread;[/CODE]

              }

              You are returning thread which is local to the function.If this function is part of larger class,you might wont to make thread a private/protected member of the class,second option is to reform your function.

              void splitter(string line,string *thread) and then use function argument to retrieve splinted string.

              Savage

              Comment

              • askalottaqs
                New Member
                • Jun 2007
                • 75

                #8
                im sorry for being such a noob :S but i really am workin my ass off to understand everything, i read and worked most of the deitel n deitel book, but its like all the resources guide me to do the simple stuff, and if i want guides to do sth complicated, they always assume im an advanced user of c++ (which i dont blame them 4) knowing that im working on the maya api. not to bore u any more, ill go on with my problem

                first of all i tried declaring it in the main class as private, and i still got that error, then i went on, kept it as private and i got this error


                1>d:\maya work\api projects (c++)\retrieveg eocmd\retrieveg eocmd\retrieveg eocmdcmd.cpp(23 0) : error C2440: '=' : cannot convert from 'std::basic_str ing<_Elem,_Trai ts,_Ax>' to 'std::string *'


                if i try to remove the declaration and only use the second solution ill get an undeclared identifier error,

                thanks again m8!

                Comment

                • Savage
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1759

                  #9
                  Originally posted by askalottaqs
                  im sorry for being such a noob :S but i really am workin my ass off to understand everything, i read and worked most of the deitel n deitel book, but its like all the resources guide me to do the simple stuff, and if i want guides to do sth complicated, they always assume im an advanced user of c++ (which i dont blame them 4) knowing that im working on the maya api. not to bore u any more, ill go on with my problem

                  first of all i tried declaring it in the main class as private, and i still got that error, then i went on, kept it as private and i got this error


                  1>d:\maya work\api projects (c++)\retrieveg eocmd\retrieveg eocmd\retrieveg eocmdcmd.cpp(23 0) : error C2440: '=' : cannot convert from 'std::basic_str ing<_Elem,_Trai ts,_Ax>' to 'std::string *'


                  if i try to remove the declaration and only use the second solution ill get an undeclared identifier error,

                  thanks again m8!
                  Can you show me that line of code?

                  Savage

                  Comment

                  • Savage
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1759

                    #10
                    Originally posted by askalottaqs
                    there u go

                    string *splitter(strin g line)
                    {
                    int strBegin=0, strLocale=0, i=1, strCount=0, j, strLngth ;
                    string str, thread[100];
                    str = line;

                    while(strLocale != string::npos)
                    {
                    strLocale = str.find(" ",strBegin) ;
                    strLngth = strLocale-strBegin;
                    thread[i] = str.substr(strB egin,strLngth);
                    strBegin = strLocale + 1;
                    strCount++;
                    i++;
                    };

                    for (j = 1; j <= strCount; j++)
                    cout << thread[j] << " ";
                    return thread;

                    }
                    Also first index in a array is 0 not 1

                    Savage

                    Comment

                    • askalottaqs
                      New Member
                      • Jun 2007
                      • 75

                      #11
                      thread[i] = str.substr(strB egin,strLngth);

                      Comment

                      • Savage
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1759

                        #12
                        Originally posted by askalottaqs
                        thread[i] = str.substr(strB egin,strLngth);
                        This is fine.
                        It should compile.

                        Perhaps you haven't included all header files you need.Which header files have you included?

                        Originally posted by askalottaqs
                        if i try to remove the declaration and only use the second solution ill get an undeclared identifier error.
                        Can you show us this too?

                        Savage

                        Comment

                        Working...