Thread: instantiated from here error

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    56

    instantiated from here error

    Hello,

    I have the following code:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    vector<string> remove_contents(const string& contents, const string& start_t, const string& end_t)
    {
    	vector<string> ret;
    	typedef string::const_iterator iter;
    	
    	iter start = contents.begin();
    	while(start != contents.end())
    	{
    		iter start = find_if(start, contents.end(), equal(start, contents.end(), start_t)) + 8;
    		iter end = find_if(start, contents.end(), equal(start, contents.end(), end_t)) - 1;
    		
    		if(start != contents.end())
    			ret.push_back(string(start, end));
    		start = end;		
    	} 
    	
    	return ret;
    }
    
    int main()
    {
    	string contents("This is a test.  I will extract <Title> the contents from between </Title> the title labels.");
    	string start_t = "<Title>";
    	string end_t = "</Title>";
    	
    	vector<string> extract = remove_contents(contents, start_t, end_t);
    	vector<string>::const_iterator i = extract.begin();
    	
    	for(i; i < extract.end(); i++)
    		cout << *i << endl;
    	
    	return 0;
    }
    I can't figure out what is causing this error.
    Code:
    In file included from /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algobase.h:67,
                     from /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/char_traits.h:41,
                     from /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/ios:41,
                     from /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/ostream:40,
                     from /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/iostream:40,
                     from split_string1.cpp:1:
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >’:
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algobase.h:825:   instantiated from ‘bool std::__equal_aux(_II1, _II1, _II2) [with _II1 = const char*, _II2 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]’
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algobase.h:964:   instantiated from ‘bool std::equal(_II1, _II1, _II2) [with _II1 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _II2 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]’
    split_string1.cpp:15:   instantiated from here
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_iterator_base_types.h:127: error: no type named ‘iterator_category’ in ‘struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >’
    In file included from /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/algorithm:62,
                     from split_string1.cpp:4:
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algo.h: In function ‘_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Predicate = bool]’:
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algo.h:4248:   instantiated from ‘_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Predicate = bool]’
    split_string1.cpp:15:   instantiated from here
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algo.h:222: error: ‘__pred’ cannot be used as a function
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algo.h:4248:   instantiated from ‘_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Predicate = bool]’
    split_string1.cpp:15:   instantiated from here
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algo.h:226: error: ‘__pred’ cannot be used as a function
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algo.h:230: error: ‘__pred’ cannot be used as a function
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algo.h:234: error: ‘__pred’ cannot be used as a function
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algo.h:242: error: ‘__pred’ cannot be used as a function
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algo.h:246: error: ‘__pred’ cannot be used as a function
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algo.h:250: error: ‘__pred’ cannot be used as a function
    In file included from /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/char_traits.h:41,
                     from /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/ios:41,
                     from /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/ostream:40,
                     from /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/iostream:40,
                     from split_string1.cpp:1:
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algobase.h: In static member function ‘static bool std::__equal<_BoolType>::equal(_II1, _II1, _II2) [with _II1 = const char*, _II2 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool _BoolType = false]’:
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algobase.h:831:   instantiated from ‘bool std::__equal_aux(_II1, _II1, _II2) [with _II1 = const char*, _II2 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]’
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algobase.h:964:   instantiated from ‘bool std::equal(_II1, _II1, _II2) [with _II1 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _II2 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]’
    split_string1.cpp:15:   instantiated from here
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algobase.h:801: error: no match for ‘operator++’ in ‘++__first2’
    /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/bits/stl_algobase.h:802: error: no match for ‘operator*’ in ‘*__first2’
    Also, does anybody know how to get rid of all the junk in the error exept for:
    Code:
    split_string1.cpp:15:   instantiated from here
    I never got all this junk using g++ in Ubuntu. It makes it difficult when trying to find where my errors are.

  2. #2
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Why would you need a vector of strings acting as a function? Not sure if it's just me, but I thought that vectors were used to create a more powerful array of variable types?
    MSDN <- Programmers Haven!

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Your algorithm is incorrect (and way more complicated than it needs to be). Look carefully at what you are doing. You are looping through every character in the contents string, and then calling find_if() for each one.

    You do not need to loop at all. Just call find() for the start_t and end_t variables, and then call substr() a couple times to cut out the unwanted text.

    Note: You will need a loop if you expect there to be several instances of the start_t and end_t strings in the contents string.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    Thanks for pointing me in the right direction.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string contents("This is a test.  I will extract <Title> the contents from between </Title> the title labels.");
    	string start_t = "<Title>";
    	string end_t = "</Title>";
    	
    	size_t pos_a, pos_b;
    	
    	pos_a = contents.find(start_t) + 8;	//the position of the end of the start of "the contents..."
    
    	pos_b = contents.find(end_t) - pos_a - 1;	//the amount of characters from pos_a to the end of "...from between"
    	
    	string extract = contents.substr(pos_a, pos_b);	//extract the characters between <Title> and </Title>
    	cout << extract;
    		
    	return 0;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A few things to note:
    • Avoid magic numbers. In this case 8 works because start_t has 7 characters and then you know that a space follows thereafter. You should use start_t.length() instead, and then trim off the leading space, if that is desirable.
    • Check that the search string was actually found.
    • Check that the occurrence of start_t comes before the occurrence of end_t. (Speaking of which, naming them start_tag and end_tag would be better, particularly since a _t suffix is often used to denote a type name.)
    • You might as well initialise pos_a and pos_b as soon as you declare them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by swappo View Post
    Also, does anybody know how to get rid of all the junk in the error exept for:
    Code:
    split_string1.cpp:15:   instantiated from here
    I never got all this junk using g++ in Ubuntu. It makes it difficult when trying to find where my errors are.
    You can't, and plainly, you shouldn't, because this is valuable diagnostic data that is necessary when debugging templates. Actually, it often times isn't enough debug data when debugging template errors, so that tells you how critical it is.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM