xcode, c++, simple class

Discussion in 'Mac Programming' started by mcMarkus, Mar 19, 2009.

  1. macrumors newbie

    Joined:
    Mar 19, 2009
    Location:
    Sweden, Linkoeping
    #1
    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???
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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.
     
  3. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #3
    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
     
  4. macrumors G4

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #4
    One more error - simple little thing, but easy to overlook. Your main file's missing a closing } after the return 0; statement.
     
  5. thread starter macrumors newbie

    Joined:
    Mar 19, 2009
    Location:
    Sweden, Linkoeping
  6. thread starter macrumors newbie

    Joined:
    Mar 19, 2009
    Location:
    Sweden, Linkoeping
    #6
    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
     
  7. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #7
    Probably because you need #include <iostream>.

    b e n
     

Share This Page