PDA

View Full Version : xcode, c++, simple class




mcMarkus
Mar 19, 2009, 08:58 AM
Hi!
I'm trying to write a very very simple program, that includes a simple class. A book object with one classdata. but the compiler just gives errors and I cant find whats wrong , can anyone help me.

////////////////////////////////////////////////
main file
////////////////////////////////////////////////
#include <iostream>
#include "TBook.h"

using namespace std;

int main() {

return 0;

////////////////////////////////////////////////
TBook.h
///////////////////////////////////////////////

#ifndef TBOOK_H
#define TBOOK_H

#include <string>

class TBook
{
public:
TBook(string title);
string GetTitle();

private:
const string Title;
};

#endif
////////////////////////////////////////////////////////////
TBook.cpp
////////////////////////////////////////////////////////////

#include "TBook.h"

Book::Book(string title);
: Title(title),
{
}

string Book::GetTitle() {
return Title;
}

////////////////////////////////////////////////////////////
xcode just gives me error when I try to compile the code.

TBook.h:19: error: expected `)' before "title"
TBook.h:20: error: 'string' does not name a type
TBook.h:25: error: 'string' does not name a type


What am I doing wrong???



lee1210
Mar 19, 2009, 09:03 AM
#include <string>
So the compiler knows what a std::string is. That may resolve the first error, re: the ), too.

-Lee

EDIT: After much posting, i saw that in your .h you do include string... but you don't use namespace std in there. You would need to use scope resolution and use std::string instead, or use the namespace.

lazydog
Mar 19, 2009, 12:20 PM
Hi

You have another error... you've only got one member being initialised in the constructor so the , isn't needed.


Book::Book(string title);
: Title(title),
{
}


b e n

wrldwzrd89
Mar 19, 2009, 12:23 PM
One more error - simple little thing, but easy to overlook. Your main file's missing a closing } after the return 0; statement.

mcMarkus
Mar 19, 2009, 01:13 PM
Thanx!

mcMarkus
Mar 20, 2009, 06:04 AM
Do anyone know why I can't write to file with:

#include "test1.h"
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
.
.
.
void TBook::SaveBook(TBook book){

ofstream printa ( "testRTF.rtf");

if ( ! printa ) {
cout << "failed to open file \n"; //HERE
exit ( EXIT_FAILURE );
}

printa << "oh yeah baby!\n";

}


the compiler says: cout was not declared in this scope

lazydog
Mar 20, 2009, 06:35 AM
Probably because you need #include <iostream>.

b e n