The I'm bored so I'm trying to learn the basics of C++ thread. Help me if you are as bored as me ;-)

Noriaki

Lifer
Jun 3, 2000
13,640
1
71
How exciting.
I'm really bored so I'm attempting to learn some basic C++

I'm making a simple class that attempts to store a mighty 1 string. Good for it yeah? Hahaha
To bad I get compile time errors and I don't know what the hell they mean.

Here's MyObj.h
class MyObj {
public:
MyObj(int n);
~MyObj();
char *getS();
void setS(char* s);
private:
char* myS;
};

Fun hey?

And here's MyObj.cc
#include "MyObj.h"

MyObj(int n) {
myS = new char[n];
}

~MyObj() {
delete myS;
}

char * MyObj::getS() {
return myS;
}

void MyObj::setS(char* s) {
myS = s;
}

Equally exciting yeah? At least it gets to be a .cc and not just a lowly .h

An now here's the one that wears the pants in the family. The .cc with the main function in it.
Wow the other guys are really jealous of it.

#include <iostream.h>
#include "MyObj.h"

void main(int argc, char *argv[]) {
MyObj *o = new MyObj(11);

o->setS("Hello World");

cout << o->getS() << endl;
}

Now if you aren't to exciting by this yet we are getting to the really really good part.
The compile time errors...

hugo% g++ Hello.cc
Undefined first referenced
symbol in file
MyObj::setS(char *) /var/tmp/ccnaOJnX.o
MyObj::getS(void) /var/tmp/ccnaOJnX.o
MyObj::MyObj(int) /var/tmp/ccnaOJnX.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

And as I'm typing this I realize that I need to compile the two togeter with a g++ *.cc

Oh...my constructor and destructor need to be member functions...good for them.

Yup now a g++ *.cc works fine and running a.out gives me Hello World.

Yup good for it.
Thanks for coming on this fun ride with me.

I'm going to play with C++ some more...so just wait I'm sure I'll have more fun stuff to post.
 

Noriaki

Lifer
Jun 3, 2000
13,640
1
71
hmmm
what's the socially acceptable way to do file I/O in C++?

In C I'd go

FILE *fh = fopen("SomeFile","SomeOptions");

then use fprintf and fscanf the same as printf and scanf but with the file handle as an extra inital parameter...

But that probably isn't the greatest way to do it in C++....

Any ideas on a better way?
 

Noriaki

Lifer
Jun 3, 2000
13,640
1
71
Hmmmm....

How do you use cin ?

int n;
cin >> n;

Works...but I can't get it to work for strings.

I have char *s;

cin >> s;
and
cin >> *s;

both give me seg faults...


Haha
Damn for someone who knows C pretty well and Java really well you'd think it'd be simple to learn C++....but no dice.
 

manly

Lifer
Jan 25, 2000
13,341
4,102
136
I'm not a C++ hacker, but you're not really using the private myS buffer in MyObj class (since all you did in the setter was assign a pointer). In fact, you might be leaking it instead. Don't you miss Java already? ;)

As far as file I/O, it looks like there are file stream abstractions (just as in Java).

Don't you have a good book to learn from?
 

Noriaki

Lifer
Jun 3, 2000
13,640
1
71


<< I'm not a C++ hacker, but you're not really using the private myS buffer in MyObj class (since all you did in the setter was assign a pointer). In fact, you might be leaking it instead. Don't you miss Java already? ;)

As far as file I/O, it looks like there are file stream abstractions (just as in Java).

Don't you have a good book to learn from?
>>



yes I think you are right.
I passed in a pointer...damn it.

Yes I do.

Yeah...someone PM'd me with the fstream syntax.

And no I don't have a good book. I'm just doing this in my spare time because I'm to tired to do anything more useful.


I love Java.
Such a simple language....the actually language of java is so small and compact...

And then they just have a huge standardly distributed library...that is excellently documented I wish C++ had something like that...

I think I like OO Perl better than C++ so far :disgust:

I'm pretty good with C...but C++ is a totally different kettle of fish...
 

Noriaki

Lifer
Jun 3, 2000
13,640
1
71
Just for fun. Lets look at this code:

StreamTokenizer inStream = new StreamTokenizer(new FileReader(filename));
int i = 0;
do {
array[i/2][i%2] = inStream.nval;
i++;
} while (inStream.nextToken() != StreamTokenizer.TT_EOF);

Now this takes a file called filename which is two columns of data and stores it into the array.

I don't have the faintest clue how I would even begin to do this in C++....

And before I did it a couple weeks ago I didn't have a clue how to do it Java...but I just looked at the API specs...and found a StreamTokenizer and the appropriate functions and fields...and the code fell into place.

Is there anything anywhere as comprehensive as the Java Lang API for C++?

Edit:

Okay..I could use a filestream and get the file line by line and parse it myself.
But that would be *alot* more work.

Since the Stream Tokenizes on whatever white space happens to be there I don't need to worry about the exact details.

Tis great.