Beginner C++ - identifier not found

  • Comp Sci
  • Thread starter jck_ccc
  • Start date
  • Tags
    Beginner C++
In summary, the author is trying to sort a vector of strings, but gets an error. He finds various online resources that are not helpful. He eventually solves the problem by creating a global function and using a namespace. If he really wants to use the class, he needs to static member it. He also provides a helpful tip on how to fix an error message.)
  • #1
jck_ccc
5
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
  • #2
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...
 
  • #3
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.
 
  • #4
EDIT: oohhh! That makes sense. I didn't know of such rules. Thank you so much for the help :)
 
  • #5
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).
 
  • #6
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);
 
  • #7
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)
 
  • #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:
//sorting.h
#pragma once
#include <string>
#include <vector>
using namespace std;

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

Related to Beginner C++ - identifier not found

1. What does the error "identifier not found" mean in C++?

When you encounter the error "identifier not found" in C++, it means that the compiler cannot find the declaration or definition of a variable, function, or class that you are trying to use in your code.

2. Why am I getting the "identifier not found" error in my C++ code?

There are a few reasons why you may be getting the "identifier not found" error in your C++ code. One common reason is that you have misspelled the identifier or used incorrect capitalization. Another reason could be that you have not included the necessary header file or namespace for the identifier. Finally, the identifier may not have been declared or defined in your code at all.

3. How can I fix the "identifier not found" error in my C++ code?

To fix the "identifier not found" error in your C++ code, you will need to identify the source of the error. Check for any spelling or capitalization mistakes in the identifier, make sure you have included the correct header file or namespace, and ensure that the identifier has been declared or defined in your code. If none of these solutions work, you may need to consult with a more experienced programmer or review your code for any other errors.

4. Can the "identifier not found" error be caused by using reserved keywords in C++?

Yes, the "identifier not found" error can be caused by using reserved keywords in C++. Reserved keywords are words that have a special meaning in the C++ language and cannot be used as identifiers. If you accidentally use a reserved keyword as an identifier, the compiler will not be able to find it and will throw an error.

5. Is there a difference between the "identifier not found" error and the "undeclared identifier" error in C++?

Yes, there is a difference between the "identifier not found" error and the "undeclared identifier" error in C++. The "identifier not found" error means that the compiler cannot find the identifier because it has not been declared or defined in the code. On the other hand, the "undeclared identifier" error means that the identifier has not been declared at all, but it may still be defined in another part of the code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
979
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
871
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
Back
Top