473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to get started?

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)

Jul 22 '05 #1
6 1337

<ro*@argus.lpl.arizona.edu> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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
Jul 22 '05 #2
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

Jul 22 '05 #3

<ro*@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
Jul 22 '05 #4
<ro*@argus.lpl.arizona.edu> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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
Jul 22 '05 #5
ro*@argus.lpl.arizona.edu 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 <ph**@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
Jul 22 '05 #6
ro*@argus.lpl.arizona.edu 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
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mark Fisher | last post by:
I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the...
13
by: Deepak Sarda | last post by:
Hello everyone. I have run into something which I believe is a bug or a shortcoming of the threading.Thread module. My program spawns 15 threads. For this I've creating a new class with...
80
by: Bibby | last post by:
Hi, I'm interested in getting started in the programming world. I've dabbled in C, C++ and VB6. Which would be the best language to focus my attention to regarding the following considerations: ...
3
by: DAMAR | last post by:
Hello Is it possible to know when a particular process started? For example: I have such situation: I need to know when a notepad.exe is started - i need to get an event of starting this...
0
by: Bob | last post by:
We have an ivr outcall application that starts calling customers at a certain time every day automatically at a predetermined time to advise those expected to receive delivery next day that their...
2
by: bz | last post by:
Hi, I need to detect at runtime if the application was started from VS 2005. Is it possible? Also, how can I know at runtime of app is built for Debug or Release? Thanks
4
by: Benjamin Hell | last post by:
Hi! I wonder whether there might be a way to find out how a Python program was started (in my case in Windows): By double clicking the file or by calling it on the "DOS" command line prompt. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.