GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Feb-2006, 03:57
Honourable Mist Honourable Mist is offline
New Member
 
Join Date: Feb 2006
Posts: 11
Honourable Mist is on a distinguished road

error: expected primary-expression before '.' token


Could someone tell me what the error message "error: expected primary-expression before '.' token" means, or at least what I'm doing wrong?
Here's the code:

CPP / C++ / C Code:
//main.cpp
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <fstream>

#include "algorithms.cpp"
#include "board.cpp"
#include "database.cpp"

using namespace std;

int main()
{
    database.createSessionfile(); // error goes here
    return (0);
}

CPP / C++ / C Code:
//database.cpp
#include <fstream>
#include <time.h>
#include "database.h"

using namespace std;

int database::createSessionfile()
{
 long name = time(NULL);
 ofstream sessionfile;
 sessionfile.open((const char*)name);
 sessionfile << name << '\n';
}

CPP / C++ / C Code:
//database.h
using namespace std;

class database {
      public:
             int createSessionfile();
};

Some irrelevant code such as other functions in class database have been omitted.

Also: hi, everyone!
Last edited by LuciWiz : 18-Feb-2006 at 09:43. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 18-Feb-2006, 06:45
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: error: expected primary-expression before '.' token


Hi Mist,

Welcome to the GID Forums.

The problem is that you have not created an instance of the class, or to be simple, you have not created a variable of the type database.

Class can be simply defined as a user defined datatype, for example, similar to string.
How do you declare a string?
string str;
Then you can access the string functions such as length etc isnt it?

Similarly, create a new variable of type database like this:
database db;
Then, you can access the member function like this:
db.createSessionfile();

Also instead of [code] and [/code] you can use [c++] and [/c++] tags.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 18-Feb-2006, 08:25
Honourable Mist Honourable Mist is offline
New Member
 
Join Date: Feb 2006
Posts: 11
Honourable Mist is on a distinguished road

Re: error: expected primary-expression before '.' token


Oh, it's so obvious now that you mention it! Thanks for the quick reply aswell!

I have probably made some equally silly mistake, for now I'm getting multiple definition errors for every single function. The only change is in main.cpp:

CPP / C++ / C Code:
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <fstream>

#include "algorithms.cpp"
#include "board.cpp"
#include "database.cpp"

using namespace std;

obj_database database; //renamed the class database to obj_database

int main()
{
    database.createSessionfile();
    return (0);
}
  #4  
Old 18-Feb-2006, 08:42
5had0w 5had0w is offline
Awaiting Email Confirmation
 
Join Date: Feb 2006
Location: Portugal
Posts: 12
5had0w is on a distinguished road

Re: error: expected primary-expression before '.' token


Hey! I know this is off topic but everytime i see one of your posts with C/C++ code, I see you always use
CPP / C++ / C Code:

using namespace std;


What is this for?

Thanks in advance,

5had0w
  #5  
Old 18-Feb-2006, 08:46
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: error: expected primary-expression before '.' token


Quote:
Originally Posted by Honourable Mist
Oh, it's so obvious now that you mention it! Thanks for the quick reply aswell!

I have probably made some equally silly mistake, for now I'm getting multiple definition errors for every single function. The only change is in main.cpp:

CPP / C++ / C Code:
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <fstream>

#include "algorithms.cpp"
#include "board.cpp"
#include "database.cpp"

using namespace std;

obj_database database; //renamed the class database to obj_database

int main()
{
    database.createSessionfile();
    return (0);
}
Add the header files to the code, rather than the cpp files..

Multiple definition errors are caused due to multiple inclusion of the header files.

For example, consider that your class is in database.h
You create another file algorithm.h where you include the database.h file.
Again in your main.cpp file, you include both database.h and algorithm.h.

What happens now?
You include the file database.h two times!!!

How to avoid this problem?
Use conditional compile statements #ifndef, #define and #endif.
For example, you can do like this:
CPP / C++ / C Code:
#ifndef _DATABASE_
#define _DATABASE_

// class definition here...

#endif
in every header file.

Now what happens?
The first time you try to include the database.h file, _DATABASE_ is not defined. So it is defined now, and the class definition is included.
The next time you try to include the database.h file, _DATABASE_ is already defined, and the code is not included.
This prevents redefinition errors.

Hope i explained it clear.
Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #6  
Old 18-Feb-2006, 08:52
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: error: expected primary-expression before '.' token


Quote:
Originally Posted by 5had0w
Hey! I know this is off topic but everytime i see one of your posts with C/C++ code, I see you always use
CPP / C++ / C Code:

using namespace std;


What is this for?

Thanks in advance,

5had0w
Namespaces are new concepts in C++, which is used to avoid name conflicts, for example, two classes with same name..

You can find more information here:
Namespaces

Inside the namespace std, there are various classes, and members.
For example, cin, cout, endl are inside the namespace std;
In order to use them, you should specify in which namespace they are present.
Either you can write like this:
using namespace std;
and use cin or cout, etc.

Or you could prefix the namespace with scope resolution operator like this:
CPP / C++ / C Code:

std::cout<<"Cout is in namespace std"<<std::endl;


Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #7  
Old 18-Feb-2006, 09:02
5had0w 5had0w is offline
Awaiting Email Confirmation
 
Join Date: Feb 2006
Location: Portugal
Posts: 12
5had0w is on a distinguished road

Re: error: expected primary-expression before '.' token


Thanks!
I've been always using since I started programming this way:

CPP / C++ / C Code:

cout << "Hi! How are you?" << endl;


and it works but I think I need to change...

Thanks again!

Cumps
  #8  
Old 18-Feb-2006, 09:12
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: error: expected primary-expression before '.' token


Quote:
Originally Posted by 5had0w
Thanks!
I've been always using since I started programming this way:

CPP / C++ / C Code:

cout << "Hi! How are you?" << endl;


and it works but I think I need to change...

Thanks again!

Cumps
That is because you must've included: iostream.h

But the standards have changed, and you should only use:
#include<iostream>
(This file defines the std namespace.)

and NOT like this:
#include<iostream.h>
The last one is deprecated, and is not defined inside a namespace.


Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #9  
Old 18-Feb-2006, 09:18
5had0w 5had0w is offline
Awaiting Email Confirmation
 
Join Date: Feb 2006
Location: Portugal
Posts: 12
5had0w is on a distinguished road

Re: error: expected primary-expression before '.' token


Oh ok! Thanks dude

And sorry for off topic.

Cumps
  #10  
Old 18-Feb-2006, 10:51
Honourable Mist Honourable Mist is offline
New Member
 
Join Date: Feb 2006
Posts: 11
Honourable Mist is on a distinguished road

Re: error: expected primary-expression before '.' token


Quote:
Add the header files to the code, rather than the cpp files..
I'm not sure what you mean.


Quote:
Multiple definition errors are caused due to multiple inclusion of the header files.

For example, consider that your class is in database.h
You create another file algorithm.h where you include the database.h file.
Again in your main.cpp file, you include both database.h and algorithm.h.

What happens now?
You include the file database.h two times!!!

I was actually really careful about only including files once

Quote:
How to avoid this problem?
Use conditional compile statements #ifndef, #define and #endif.
For example, you can do like this:
CPP / C++ / C Code:
#ifndef _DATABASE_
#define _DATABASE_

// class definition here...

#endif
in every header file.

Now what happens?
The first time you try to include the database.h file, _DATABASE_ is not defined. So it is defined now, and the class definition is included.
The next time you try to include the database.h file, _DATABASE_ is already defined, and the code is not included.
This prevents redefinition errors.

I did this with all my headers, but the problem remains! I even tried doing the same thing with the cpp-files aswell, but no change.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with enums and function program rho C++ Forum 8 27-Jun-2005 19:13
Replacing N'th token in a file. kobi_hikri C Programming Language 2 24-Jun-2005 02:00
Can enum have same name as class? crystalattice C++ Forum 3 08-Dec-2004 16:43
Floating point exception error (Note-long post) crystalattice C++ Forum 16 11-Sep-2004 07:37
storing a token pointer as a string CoreLEx C Programming Language 1 07-Oct-2003 11:33

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:16.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.