How to get started?

R

ron

I have a background in C. I downloaded and installed g++ on my Sun
system and attempted to compile a sample program in C++ and it doesn't
work. The gcc compiler works pretty much out of the box, but the g++
compiler doesn't appear to find everything it needs...
Since im new to c++, I don't know exactly where to get started.
Is there some guidance out there on how one get's started using c++ and
the various object libraries I hear referenced such as gnu's object
library (which I assume was included with the g++ compiler).
The example source came from http://www.yrl.co.uk/~phil/stl/stl.htmlx
as example 4.3.
Please post reply, do not email.
Ron

~> g++ -O example_4_3.cxx -o x
example_4_3.cxx:27: ISO C++ forbids declaration of `ostream' with no
type
example_4_3.cxx:27: `ostream' is neither function nor member function;
cannot
be declared friend
example_4_3.cxx:27: syntax error before `&' token
example_4_3.cxx:35: 'string' is used as a type, but is not defined as a
type.
example_4_3.cxx: In constructor `TaskObject::TaskObject(const char*,
unsigned
int)':
example_4_3.cxx:30: `process_name' undeclared (first use this function)
example_4_3.cxx:30: (Each undeclared identifier is reported only once
for each
function it appears in.)
example_4_3.cxx: At global scope:
example_4_3.cxx:39: syntax error before `&' token
example_4_3.cxx: In function `int main(int, char**)':
example_4_3.cxx:57: `priority_queue' undeclared (first use this
function)
example_4_3.cxx:57: syntax error before `,' token
example_4_3.cxx:64: `task_queue' undeclared (first use this function)
example_4_3.cxx:67: `cout' undeclared (first use this function)
example_4_3.cxx:67: `endl' undeclared (first use this function)
 
M

Mike Wahler

I have a background in C. I downloaded and installed g++ on my Sun
system and attempted to compile a sample program in C++ and it doesn't
work. The gcc compiler works pretty much out of the box, but the g++
compiler doesn't appear to find everything it needs...
Since im new to c++, I don't know exactly where to get started.

Get some books. See www.accu.org for peer reviews and recommendations.

If you want help with those error messages, you'll need to
post your code.

-Mike
 
R

ron

Here's some sample source:
#include <stdlib.h>
#include <vector>

int main (int argc, char *argv[]) {
vector<int> v;
}

Here's the sample output:
~> g++ -O test.c -o test
test.c: In function `int main(int, char**)':
test.c:5: `vector' undeclared (first use this function)
test.c:5: (Each undeclared identifier is reported only once for each
function
it appears in.)
test.c:5: syntax error before `>' token

Im thinking that it's not finding all the libraries, but I don't know
for sure.
Ron
 
M

Mike Wahler

Here's some sample source:
#include <stdlib.h>

You're not using anything from this header.
#include <vector>

int main (int argc, char *argv[]) {

You're not using these parameters. You needn't
declare them.
vector<int> v;
}

Here's the sample output:
~> g++ -O test.c -o test
test.c: In function `int main(int, char**)':
test.c:5: `vector' undeclared (first use this function)
test.c:5: (Each undeclared identifier is reported only once for each
function
it appears in.)
test.c:5: syntax error before `>' token

Im thinking that it's not finding all the libraries, but I don't know
for sure.

It's not finding the definition of type 'vector'.


You *really* need some books.

#include <vector>

int main ()
{
std::vector<int> v;
return 0;
}

-Mike
 
E

E. Robert Tisdale

I have a background in C. I downloaded and installed g++ on my Sun
system and attempted to compile a sample program in C++ and it doesn't
work. The gcc compiler works pretty much out of the box, but the g++
compiler doesn't appear to find everything it needs...
Since im new to c++, I don't know exactly where to get started.
Is there some guidance out there on how one get's started using c++ and
the various object libraries I hear referenced such as gnu's object
library (which I assume was included with the g++ compiler).
The example source came from http://www.yrl.co.uk/~phil/stl/stl.htmlx
as example 4.3.
cat example_4_3.cxx
// Phil Ottewell's STL Course -
// http://www.yrl.co.uk/~phil/stl/stl.htmlx
//
// Example 4.3 © Phil Ottewell 1997 <[email protected]>
//
// Purpose:
// Demonstrate use of priority_queue container adaptor
// by using a task/priority structure
//

// ANSI C Headers
#include <stdlib.h>

// C++ STL Headers
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#ifdef _WIN32
using namespace std;
#endif

class TaskObject {
private:
unsigned int priority;
std::string process_name;
public:
friend
class PrioritizeTasks;
friend
std::eek:stream & operator<<(
std::eek:stream &os, const TaskObject &task);
TaskObject(const char* pname = "", unsigned int prio = 4):
priority(prio), process_name(pname) { }
};

// Friend function
// for "printing" TaskObject to an output stream
std::eek:stream & operator<<(
std::eek:stream &os, const TaskObject &task ) {
return os << "Process: " << task.process_name
<< " Priority: " << task.priority;
}

// Friend class with function object
// for comparison of TaskObjects
class PrioritizeTasks {
public:
int operator()(const TaskObject &x, const TaskObject &y) {
return x.priority < y.priority;
}
};

int main(int argc, char *argv[]) {
std::priority_queue<
TaskObject, std::vector<TaskObject>, PrioritizeTasks>
task_queue;
TaskObject tasks[] = {
"JAF", "ROB", "PHIL", "JOHN",
TaskObject("OPCOM", 6), TaskObject("Swapper",16),
TaskObject("NETACP",8), TaskObject("REMACP",8) };

for (size_t i = 0; i < sizeof(tasks)/sizeof(tasks[0]); ++i)
task_queue.push( tasks );
while (!task_queue.empty()) {
std::cout << task_queue.top() << std::endl;
task_queue.pop();
}
std::cout << std::endl;
return EXIT_SUCCESS;
}
g++ -O -o example_4_3 example_4_3.cxx
./example_4_3
Process: Swapper Priority: 16
Process: NETACP Priority: 8
Process: REMACP Priority: 8
Process: OPCOM Priority: 6
Process: PHIL Priority: 4
Process: JOHN Priority: 4
Process: ROB Priority: 4
Process: JAF Priority: 4
g++ --version
g++ (GCC) 3.4.1
 
P

Paavo Helde

(e-mail address removed) wrote in @f14g2000cwb.googlegroups.com:
~> g++ -O example_4_3.cxx -o x
example_4_3.cxx:27: ISO C++ forbids declaration of `ostream' with no
type
example_4_3.cxx:27: `ostream' is neither function nor member function;

Seems like std:: namespace has been forgotten. Correct usage is:

#include <iostream>
#include <ostream>

std::cout << "Hello world!\n";

IOW, you will need to prepend std:: to all STL symbols in the code, or
alternatively use

using namespace std;

near the top of all source files. In the link you gave this line is
included in #ifdef _WIN32 guards, which I'm afraid won't work on Sun ;)
So maybe it's better to look for some more adequate learning material, I
would suggest Accelerated C++ by Koenig & Moo.

HTH
Paavo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top