1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Beginner C++ - identifier not found

  1. Nov 20, 2009 #1
    Any help is greatly appreciated

    1. The problem statement, all variables and given/known data

    I am trying to sort a string of vectors in the proper order. My problem does not involve theory, it involves syntax errors. Moving from Java to C++ is sure a pain..

    I have 2 c++ files, sorting.h and sorting.cpp

    I keep getting this error:
    sorting.cpp(88) : error C3861: 'sortString': identifier not found

    Code (Text):

    //sorting.h
    #pragma once
    #include <string>
    #include <vector>
    using namespace std;

    class sorting
    {
    public:
        sorting(void);
        ~sorting(void);
        vector<string> sortString(vector<string>);
        bool compString(string, string);
    };
    Code (Text):
    //sorting.cpp
    #include "sorting.h"
    #include "stdlib.h"
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <string>

    using namespace std;

    sorting::sorting(void){}
    sorting::~sorting(void){}

    //implemented with insertion sort in mind
    vector<string> sorting::sortString(vector<string> s){
        int current = 1;
        string temp;
        int walker;
        int size = s.size();
        while (current < size){
            temp = s[current];
            walker = current -1;
            while ((walker >= 0) && compString(temp,s[walker])){
                s[walker+1] = s[walker];
                walker = walker - 1;
            }
            s[walker+1] = temp;
            current++;
        }
        return s;
    }
       
    //string comparator made easier
    bool sorting::compString(string s1, string s2){
        if (s1.compare(s2) == 1)
            return true;
        return false;
    }

    int main() {
        vector<string> vec;
        vec.push_back("asdf"); vec.push_back("abcd"); vec.push_back("zzzz");
        vector<string> vec2 = sortString(vec); //errors out RIGHT HERE

        /*for (int i = 0; i < vec2.size(); i++){
            cout << vec2[i];
        }*/

        return 0;
    }

    2. Relevant equations

    The code will sort a vector of type string via insertion sort.

    3. The attempt at a solution

    Googled for the past hour, nothing
    Looked at old notes, no help
     
  2. jcsd
  3. Nov 20, 2009 #2

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    In the line where the error is reported:
    vector<string> vec2 = sortString(vec);
    you are trying to invoke the function sortString. The error is (correctly) telling you that no such function exists.


    But, you say, I did declare and define the member function earlier, with
    class sorting
    {
    // ...
    vector<string> sortString(vector<string>);
    // ...
    };

    Now, ponder about that....
     
  4. Nov 20, 2009 #3

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Done pondering yet?

    (If you haven't pondered and just skipped down, then you really should! :tongue: Working out the meaning of an error message is a good exercise. What I described is exactly the process I would go through when faced with an error)









    The problem is that sortString was declared as a member function of class sorting. It only exists in that scope. This means you can only ever access this function in three circumstances:
    1. The call appears somewhere inside the scope of class sorting. (e.g. sorting::compString could use sorting::sortString in that fashion)
    2. The function is invoked as a member function of an instance of class sorting -- just like how you invoked the member function vector<string>::push_back
    3. The function is accessed with the scope operator as sorting::sortString. This is only good for static functions and esoteric pointer-to-member usage.
     
  5. Nov 20, 2009 #4
    EDIT: oohhh!!! That makes sense. I didn't know of such rules. Thank you so much for the help :)
     
  6. Nov 20, 2009 #5

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    So what's the fix?


    Well, what you probably really meant to do is to write a global function. class sorting is not any sort of object or data type -- it's simply a wrapper for a collection of functions. Functions in C++ don't need wrappers like they do in java. You probably just wanted your header file to be
    Code (Text):
    //sorting.hh
    #pragma once
    #include <string>
    #include <vector>
    using namespace std;

    vector<string> sortString(vector<string>);
    bool compString(string, string);
    However, if you really like the wrapper, then the normal way to do it in C++ is through a namespace:

    Code (Text):

    //sorting.hh
    #pragma once
    #include <string>
    #include <vector>
    using namespace std;

    namespace sorting
    {
        vector<string> sortString(vector<string>);
        bool compString(string, string);
    };
    And now you can access the functions via sorting::sortString(vec). Or, you could drop the sorting:: by putting using namespace sorting someplace earlier.


    If you really, really want it in a class, then you want static members, such as
    Code (Text):

    //sorting.hh
    #pragma once
    #include <string>
    #include <vector>
    using namespace std;

    class sorting
    {
    public:
        static vector<string> sortString(vector<string>);
        static bool compString(string, string);
    };
    And the functions can (only) be accessed via something like sorting::sortString(vec).
     
  7. Nov 20, 2009 #6

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Oh, and one last oddity. If you really, really did mean for class sorting to be a kind of data object (e.g. maybe each module of your bigger program wants to keep track of its sorting statistics) then your original piece of code is fine. But before you can use any of the sorting routines, you would first have to instantiate an object, such as

    vector<string> vec;
    sorting sorter;
    sorter.sortString(vec);
     
  8. Nov 20, 2009 #7

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    One last thing: C++ provides a standard sort routine. You would use it by


    #include <string>
    #include <vector>
    #include <algorithm> // for sort
    using namespace std;

    int main() {
    vector<string> vec;
    // Fill the vector
    sort(vec.begin(), vec.end());​
    }

    Which sorts the vector based on the < operator (which is correctly defined for string objects). There's another version of std::sort that takes a third parameter which is a "function object" that defines how to compare two objects.

    (Okay, I'm really done now with that burst of replies)
     
  9. Nov 20, 2009 #8
    I never thought about the std sort. Either way, you have really helped me. I learned so much :) Thank you1

    (I used this fix)
    Code (Text):
    //sorting.h
    #pragma once
    #include <string>
    #include <vector>
    using namespace std;

    vector<string> sortString(vector<string>);
    bool compString(string, string);
     
    Last edited: Nov 20, 2009
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Beginner C++ - identifier not found
  1. C++ . (Replies: 19)

  2. Python Beginner (Replies: 3)

Loading...