C++ stdlib

Hi,

I tried to compile a simple C++ program that uses stdlib. First it did not find stdlib. So I installed all the C++ development pattern packages in yast. Afterwards, algo.h was still missing, so I downloaded it from koders.com. But now it gives me a compile error. Could it be that algo.h is not compatible, what package contains it and how can I install it? Many thanks, I am at a loss!

linux-c6m1:/home/dorien/C++/DMusic # gcc Main.c++           
In file included from Main.c++:5:                           
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:29:22: error: includes.h: No such file or directory
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:30:20: error: buffer.h: No such file or directory  
Main.c++:6:20: error: vector.h: No such file or directory                                              
Main.c++:7:22: error: iostream.h: No such file or directory                                            
In file included from Main.c++:5:                                                                      
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:83: error: variable or field ‘buf_put_algolist’ declared void
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:83: error: ‘buffer’ was not declared in this scope           
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:83: error: ‘buf’ was not declared in this scope              
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:83: error: expected primary-expression before ‘localalgos’   
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:85: error: ‘buffer’ was not declared in this scope           
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:85: error: ‘buf’ was not declared in this scope              
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:85: error: expected primary-expression before ‘localalgos’   
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:86: error: expected primary-expression before ‘int’          
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:86: error: initializer expression list treated as compound expression
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:87: error: ‘buffer’ was not declared in this scope                   
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:87: error: ‘buf’ was not declared in this scope                      
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:87: error: expected primary-expression before ‘localalgos’           
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:88: error: expected primary-expression before ‘int’
/usr/lib64/gcc/x86_64-suse-linux/4.3/include/algo.h:88: error: initializer expression list treated as compound expression
Main.c++: In function ‘int main(int, char**)’:
Main.c++:18: error: ‘vector’ was not declared in this scope
Main.c++:18: error: expected primary-expression before ‘int’
Main.c++:18: error: expected `;' before ‘int’
Main.c++:20: error: ‘cin’ was not declared in this scope
Main.c++:21: error: ‘music’ was not declared in this scope
Main.c++:22: error: ‘music’ was not declared in this scope
Main.c++:22: error: ‘sort’ was not declared in this scope
Main.c++:23: error: expected ‘,’ or ‘;’ before ‘)’ token
Main.c++:25: error: ‘cout’ was not declared in this scope


#include <stdlib.h>
#include <string.h>
#include <algo.h>
#include <vector.h>
#include <iostream.h>

int main( int argc, char **argv )
{
	vector<int> music;
	int input;
	while (cin >> input)
		music.push_back (input);
	sort (music.begin(), music.end());
	int aantalnoten = music.size());
	for (int i=0; i < aantalnoten; i++)
		cout << music* << "
";
}


You need to update your knowledge about C++.

Firstly the new style is

#include <vector>

no .h

Secondly you need

using namespace std;

to have cin and cout. And to get sort you include <algorithm> instead of <algo.h>. <stdlib> doesn’t exist.

Also you have a syntax error on the line with music.size()), extra ) there.

See:

vector - C++ Reference
sort - C++ Reference

with those changes I could compile without error messages, but I didn’t check to see if it runs correctly. Good luck.

Super!

Thank you so much, I will give it a try as soon as I get home.
I did already try with or without the .h to no avail. I guess I got confused reading posts like this: stdio.h and stdlib.h appear to be missing [Archive] - Ubuntu Forums

The script however comes pretty literally from http://www4.cs.umanitoba.ca/~jacky/Robotics/Papers/c++-tutorial.pdf

I guess that’s not the best tutorial.

I’ll let you know how it goes!

I does not seem to work yet:


#include <string>
#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;


int main( int argc, char **argv )
{
	vector<int> music;
	int input;
	while (cin >> input)
		music.push_back (input);
	sort (music.begin(), music.end());
	int aantalnoten = music.size();
	for (int i=0; i < aantalnoten; i++)
		cout << music* << "
";
}


gives me:

/tmp/ccdlRFcn.o: In function `__static_initialization_and_destruction_0(int, int)':
Main.c++:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
Main.c++:(.text+0x28): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccdlRFcn.o: In function `main':
Main.c++:(.text+0x81): undefined reference to `std::cin'
Main.c++:(.text+0x86): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
Main.c++:(.text+0x9c): undefined reference to `std::basic_ios&lt;char, std::char_traits&lt;char&gt; &gt;::operator void*() const'
Main.c++:(.text+0xf1): undefined reference to `std::cout'
Main.c++:(.text+0xf6): undefined reference to `std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;::operator&lt;&lt;(int)'
Main.c++:(.text+0x103): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccdlRFcn.o: In function `std::vector&lt;int, std::allocator&lt;int&gt; &gt;::_M_check_len(unsigned long, char const*) const':
Main.c++:(.text._ZNKSt6vectorIiSaIiEE12_M_check_lenEmPKc[std::vector&lt;int, std::allocator&lt;int&gt; &gt;::_M_check_len(unsigned long, char const*) const]+0x43): undefined reference to `std::__throw_length_error(char const*)'
/tmp/ccdlRFcn.o: In function `__gnu_cxx::new_allocator&lt;int&gt;::allocate(unsigned long, void const*)':
Main.c++:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[__gnu_cxx::new_allocator&lt;int&gt;::allocate(unsigned long, void const*)]+0x32): undefined reference to `std::__throw_bad_alloc()'
Main.c++:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[__gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*)]+0x43): undefined reference to `operator new(unsigned long)'
/tmp/ccdlRFcn.o: In function `__gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long)':
Main.c++:(.text._ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim[__gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long)]+0x19): undefined reference to `operator delete(void*)'
/tmp/ccdlRFcn.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Something seems wrong with the io function. I made the changes you suggested.

Sorry if this is the wrong forum to ask. Perhaps I should repost on a c++ forum :-)*

Works for me:

$ cat x.cc
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int main( int argc, char **argv )
{
        vector<int> music;
        int input;
        while (cin >> input)
                music.push_back (input);
        sort (music.begin(), music.end());
        int aantalnoten = music.size();
        for (int i=0; i < aantalnoten; i++)
                cout << music* << "
";
}
$ g++ -o x x.cc
$

I can just compile with gcc right? Or should I use g++ -0 x?

You need to use g++ to correctly link with the C++ libraries. That’s what those errors were about, it couldn’t find the libraries.

It worked g++!

Thank you so much!

On Fri, 25 Jun 2010 11:26:01 GMT, Dorax <Dorax@no-mx.forums.opensuse.org>
wrote:

>
>I does not seem to work yet:
>
>
>Code:
>--------------------
>
> #include <string>
> #include <algorithm>
> #include <vector>
> #include <iostream>
>
> using namespace std;
>
>
> int main( int argc, char *argv )
> {
> vector<int> music;
> int input;
> while (cin >> input)
> music.push_back (input);
I did not think that this would be the right call for sort in a
vector<int> template.
> sort (music.begin(), music.end());
I am however quite certain that there is a much more c++ iterator to walk
the vector<int>.
> int aantalnoten = music.size();
> for (int i=0; i < aantalnoten; i++)
> cout << music
<< "
";
> }
>
>
>--------------------
>
>
*