LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-04-2004, 11:55 AM   #1
lokee
Member
 
Registered: Feb 2003
Distribution: Gentoo
Posts: 381

Rep: Reputation: 30
C++ / Can't access class members through the "." operator


Hi,
This is really weird... I've been looking at this problem from different angles, but still: no solution.
Here's an example code:
Code:
class lo {public: int o;};
lo li;
li.o = 15;
This gives me(g++):
Code:
error: syntax error before `.' token
The version of g++ is:
g++ (GCC) 3.3.3 20040217 (Gentoo Linux 3.3.3, propolice-3.3-7).

Thanks in advance.
 
Old 07-04-2004, 12:11 PM   #2
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 33
Thumbs down

Hi,
Of course you can not this compile. You are not allowed to initialize attribute o outside a function.

for example you should do this:
Code:
class lo {public: int o;};

int main(int argc, char **argv)
{
	lo li;
	li.o = 15;
	return 0;
}
or in constructor like this:
Code:
lo::lo():o(15){}


Try it.


--Michal
 
Old 07-04-2004, 12:58 PM   #3
lokee
Member
 
Registered: Feb 2003
Distribution: Gentoo
Posts: 381

Original Poster
Rep: Reputation: 30
Talking



This is funny!
Of course it's inside a function: the main().
Sorry for not writing it, I taught it was unnecessary to mention that.

Keep cool.
 
Old 07-04-2004, 01:14 PM   #4
luxitan
Member
 
Registered: Aug 2003
Location: Portugal
Distribution: Gentoo
Posts: 78

Rep: Reputation: 15
you are doing something wrong, that code compiles fine in my machine, put the whole code that gives the error.
 
Old 07-04-2004, 02:55 PM   #5
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Since I am learning C++ could some one please tell me the advantage of doing:
Code:
lo::lo():o(15){}
over:
Code:
lo::lo(){this->o=15;}
Thanks in advance.

Btw, there is nothing wrong with your code, compiles fine. Did you name your source file with a .cpp extension?
 
Old 07-04-2004, 05:00 PM   #6
luxitan
Member
 
Registered: Aug 2003
Location: Portugal
Distribution: Gentoo
Posts: 78

Rep: Reputation: 15
in the code you gave I don't think there is a difference, but if you have something like this:

Code:
class A
{
  int var;

public :

  A(int a)
  {
    var = a;
  }
};

class B
{
  
  A a;

public :

  B() : a(5)
  {

  }

};

int main()
{
  B b;
}
it only compiles in the first notation
 
Old 07-05-2004, 04:58 AM   #7
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Nerd


The difference is mostly in efficiency. By calling the assignment operator your member object has already been constructed with its default ctor. This doesn't matter for built-ins but if your member, o, is a large class with a detailed construction process of its own, e.g. multiple memory allocations, you can end up performing these operations upto 3 times, depending on your assignment operator.

HTH
 
Old 07-05-2004, 12:06 PM   #8
lokee
Member
 
Registered: Feb 2003
Distribution: Gentoo
Posts: 381

Original Poster
Rep: Reputation: 30
Ok thanks to everyone for trying to help me...
How simple:
There was a missing "{", stupid me! (Was trying everything in the same file)

Best regards,

Last edited by lokee; 07-05-2004 at 12:13 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"undefined reference" to a template class function btb Programming 3 08-25-2005 05:02 PM
does any members in this forum like poetry or something that sounds "poetic" alred General 8 06-03-2005 12:07 PM
Perl question concerning the "next" operator joesbox Programming 3 03-27-2005 08:59 PM
defining "+" operator for new datatype TexasDex Programming 7 04-03-2004 10:39 AM
"X-MS" cant open because "x-Multimedia System" cant access files at "smb&qu ponchy5 Linux - Networking 0 03-29-2004 11:18 PM


All times are GMT -5. The time now is 03:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration