Problem with STL.

Started by
3 comments, last by cdev1990 11 years ago

hi everyone.

I'm having some strange problems with the STL in C++.

I have this code:


for(i=0; i<theTree.size(); ++i)
	{	//Look if the rule already exits
		for(j=leftSides.begin(); j!=leftSides.end(); ++j)
		{
			if((*j).rule.type==theTree[0].type && strcmp((*j).rule.text, theTree[0].text))
			{
				break;
			}
		}
		//If it exists add the lefts side
		if(j!=leftSides.end())
		{
			(*j).parts.insert(theTree[1]);
		}
		else
		{
			//If it does not exists, push it and add left side
			tempList.rule=theTree[0];
			tempSet.insert(theTree[1]);
			tempList.parts=tempSet;
			leftSides.pushBack(tempList);
		}

	}

The definitions are:


std::vector<std::vector<struct token> > theTree;
std::vector<struct parts_list>leftSides;
std::set<struct token> tempSet;
struct parts_list tempList;

All this is part of a little compiler i'm implementing, but when i try to compile the code it gives me this:


g++ main.cpp cLexicParser.cpp cSyntaxParser.cpp -o grammar
cSyntaxParser.cpp: En la función miembro ‘void cSyntaxParser::PrintTree()’:
cSyntaxParser.cpp:75:46: aviso: formato ‘%d’ espera un argumento de tipo ‘int’, pero el argumento 2 es de tipo ‘std::vector<std::vector<token> >::size_type {aka long unsigned int}’ [-Wformat]
cSyntaxParser.cpp: En la función miembro ‘bool cSyntaxParser::createLeftSides()’:
cSyntaxParser.cpp:142:14: error: ‘class std::vector<parts_list>’ no tiene un miembro llamado ‘pushBack’
cSyntaxParser.cpp: En el ámbito global:
cSyntaxParser.cpp:148:42: error: no hay una función miembro ‘bool cSyntaxParser::createRightSides()’ declarada en la clase ‘cSyntaxParser’
cSyntaxParser.cpp:153:41: error: no hay una función miembro ‘void cSyntaxParser::LS(token)’ declarada en la clase ‘cSyntaxParser’
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_tree.h:65:0,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/set:60,
                 from structs.h:3,
                 from cSyntaxParser.h:1,
                 from cSyntaxParser.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_function.h: En la función miembro ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [con _Tp = token]’:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_tree.h:1277:4:   instanciado desde ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [con _Key = token, _Val = token, _KeyOfValue = std::_Identity<token>, _Compare = std::less<token>, _Alloc = std::allocator<token>]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_set.h:410:29:   instanciado desde ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [con _Key = token, _Compare = std::less<token>, _Alloc = std::allocator<token>, typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<token>, std::set<_Key, _Compare, _Alloc>::value_type = token]’
cSyntaxParser.cpp:140:32:   instanciado desde aquí
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_function.h:236:22: error: no hay coincidencia para ‘operator<’ en ‘__x < __y’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_function.h:236:22: nota: los candidatos son:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_pair.h:207:5: nota: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_iterator.h:291:5: nota: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_iterator.h:341:5: nota: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_tree.h:866:5: nota: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_set.h:713:5: nota: template<class _Key, class _Compare, class _Alloc> bool std::operator<(const std::set<_Key, _Compare, _Alloc>&, const std::set<_Key, _Compare, _Alloc>&)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_multiset.h:696:5: nota: template<class _Key, class _Compare, class _Alloc> bool std::operator<(const std::multiset<_Key, _Compare, _Alloc>&, const std::multiset<_Key, _Compare, _Alloc>&)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/bits/stl_vector.h:1290:5: nota: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)

which is quite chaotic.

I can't use any pair utility because it also yield this same error message.

Can someone tell me what's going on?

BTW, sorry, the compiler is in spanish, although i think it can be understood.

Advertisement

I just looked at the first one. This is just a wild guess because you provide no line numbers, and some of the code is missing. But - it looks like the first error is complaining that it's expecting an int instead of a size_t. Is your i loop variable of type int? Change it to size_t.

Read the warnings and errors. They're there to tell you what's wrong with your code. I only know English, but from reading the errors, I can see that:

1) You're calling pushBack. That's a problem, because it should be push_back

2) It's also complaining about createRightSides and LS in cSyntaxParser (it sounds like you're trying to define the member functions without first declaring them)

3) That first warning you're getting is because you're using %d in a formatting function (printf?), which expects an int (but you're not passing it an int, (you're probably passing it theTree.size(), which should be casted to an int))

4) You're getting a whole lot of errors towards the end there because you're trying to make a std::set of tokens (std::set requires that operator< work for the type it's storing, and judging by the errors, there's no operator< for token) (you can also give a custom comparator to std::set if the type does not have operator< or you want to use a custom comparing function)

I know C++ compiler error messages can be scary, but if you take a moment to try to read them and understand them (even if they're not in a language you speak), there's a lot you can learn from them!

Also, is there a reason you're using so much C-like code in your C++?

Also, I assume you mean the C++ Standard Library and not the STL (sorry to nitpick).

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

hi again.

Thanks for your answers.

The real problem i was having trouble with is number 4. I forgot to define the operator <.

It is hard to read the errors because, first, it looks chaotic in the terminal, second, there is a lot of text for one error(the last one).

Anyway, this was really helpful. Thanks a lot.

Some of those warnings and errors are easy to fix, because the compiler is telling you exactly what the problem is. So why not fix them instead of just posting all that stuff if you're only having a problem with one of them? Yes, of course it's going to look chaotic if you're trying to solve problem 4 without first addressing problems 1 through 3. This is basic stuff.

This topic is closed to new replies.

Advertisement