How to get started?



 DEVELOP > c-Plus-Plus > How to get started?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 14 Dec 2004 09:41:26 PM
Object: How to get started?
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)
.

User: "Mike Wahler"

Title: Re: How to get started? 14 Dec 2004 10:05:54 PM
<ron@argus.lpl.arizona.edu> wrote in message
news:1103082086.549200.244400@f14g2000cwb.googlegroups.com...

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
.
User: ""

Title: Re: How to get started? 14 Dec 2004 10:11:49 PM
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
.
User: "Sharad Kala"

Title: Re: How to get started? 14 Dec 2004 10:17:57 PM
<ron@argus.lpl.arizona.edu> wrote in message

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

You don't require this header for the posted code.

#include <vector>

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

Make that - std::vector<int> v;

}

Read about namespaces (std is a namespace).
Sharad
.

User: "Mike Wahler"

Title: Re: How to get started? 14 Dec 2004 10:20:21 PM
<ron@argus.lpl.arizona.edu> wrote in message
news:1103083908.973960.183740@z14g2000cwz.googlegroups.com...

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
.



User: "Paavo Helde"

Title: Re: How to get started? 15 Dec 2004 04:37:16 PM
wrote in news:1103082086.549200.244400
@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
.

User: "E. Robert Tisdale"

Title: Re: How to get started? 14 Dec 2004 10:52:17 PM
wrote:

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 <phil@yrl.co.uk>
//
// 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::ostream & operator<<(
std::ostream &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::ostream & operator<<(
std::ostream &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[i] );
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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER