Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mcMarkus

macrumors newbie
Original poster
Mar 19, 2009
3
0
Sweden, Linkoeping
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Code:
#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

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Hi

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

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

b e n
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
One more error - simple little thing, but easy to overlook. Your main file's missing a closing } after the return 0; statement.
 

mcMarkus

macrumors newbie
Original poster
Mar 19, 2009
3
0
Sweden, Linkoeping
save2file

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.