Comp Sci Beginner C++ - identifier not found

  • Thread starter Thread starter jck_ccc
  • Start date Start date
  • Tags Tags
    Beginner C++
AI Thread Summary
The discussion centers on a C++ syntax error where the function 'sortString' is not recognized due to its declaration as a member of the 'sorting' class. The solution involves either using a global function or defining the functions as static members within the class. Alternatively, employing a namespace allows access to the functions without instantiating the class. The conversation also highlights the use of C++'s standard sort function as a simpler sorting solution. Overall, understanding the scope and access rules in C++ is crucial for resolving such identifier issues.
jck_ccc
Messages
5
Reaction score
0
Any help is greatly appreciated

Homework Statement



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:
//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:
//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;
}

Homework Equations



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

The Attempt at a Solution



Googled for the past hour, nothing
Looked at old notes, no help
 
Physics news on Phys.org
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...
 
Done pondering yet?

(If you haven't pondered and just skipped down, then you really should! :-p 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.
 
EDIT: oohhh! That makes sense. I didn't know of such rules. Thank you so much for the help :)
 
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:
//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:
//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:
//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).
 
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);
 
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)
 
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:
//sorting.h
#pragma once
#include <string>
#include <vector>
using namespace std;

vector<string> sortString(vector<string>);
bool compString(string, string);
 
Last edited:

Similar threads

Replies
3
Views
1K
Replies
2
Views
2K
Replies
2
Views
2K
Replies
14
Views
5K
Replies
2
Views
3K
Replies
1
Views
1K
Replies
8
Views
1K
Replies
5
Views
3K
Replies
3
Views
1K
Back
Top