LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-23-2007, 06:57 AM   #1
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Rep: Reputation: 34
Basic C++ question


I wrote two programs:

This one works:

Quote:
#include <iostream>
using namespace std;

class class1
{
public:
void func()
{

int i=0;
for(i=0;i<=10;i++)
cout<<i<<endl;
}

};


int main()
{
class1 cla;
cla.func();
return 0;
}

but this one doesn't:

Quote:
#include <iostream>
using namespace std;

class class1
{
int i=0;
for(i=0;i<=10;i++)
cout<<i<<endl;
};


int main()
{
class1 cla;
return 0;
}


I get this error:

Code:
$ g++ class2.cpp
class2.cpp:7: ISO C++ forbids initialization of member `i'
class2.cpp:7: making `i' static
class2.cpp:7: ISO C++ forbids in-class initialization of non-const static
   member `i'
class2.cpp:8: parse error before `for'
class2.cpp:8: syntax error before `<=' token
class2.cpp:8: syntax error before `++' token


There isn't much difference between the programs as far as I can see.I execute one of them with a function and don't with another. What is the difference? I can't figure out what is wrong through the errors.

Last edited by gregorian; 02-23-2007 at 07:01 AM.
 
Old 02-23-2007, 07:00 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
there's a huge difference... you're trying to write dynamic code inside a class, which doesn't make sense. you need to define functions inside classes, and they define code to actually process. you don't "run" a class, as i'm sure you're aware, it's a container, not a function. so that code you have there would never run, and is just in the wrong place.
 
Old 02-23-2007, 07:12 AM   #3
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
Ok, I included cla.class1(); in the main() function. Why isn't it invoking the default constructor? I'm still getting the same error message, with this as a new addition:

class2.cpp:15: calling type `class1' like a method
 
Old 02-23-2007, 06:38 PM   #4
7stud
LQ Newbie
 
Registered: Feb 2007
Posts: 22

Rep: Reputation: 15
Quote:
Originally Posted by gregorian
Ok, I included cla.class1(); in the main() function.
What does that mean?

You are using illegal syntax in your class--it's as simple as that. In a class you can have things like declarations of variables, e.g.:
Code:
int num;
declarations of functions, e.g.:
Code:
int myFunc(int n);
and blocks of code. One example of a legal block of code is a group of statements that is surrounded by braces and serves as a function definition and is immediately preceded by the function header(return type, function name, parameters), e.g.:
Code:
int myFunc(int n)
{
     cout<<n<<endl;
     return 2*n;
}
This code:
Code:
class class1
{
   int i=0;
   for(i=0;i<=10;i++)
   cout<<i<<endl;
};
violates about every syntax rule in C++. The line:

int i = 0;

looks like a declaration of a member variable, but it is an illegal statement; you can't assign a value to a member variable in a class definition(one exception). This line:

for(i=0;i<=10;i++)

is strictly illegal in a class definition; it's not a declaration of a member variable; it's not a function declaration; and it's not inside a block of code that is part of a function definition.

Quote:
Why isn't it invoking the default constructor?
Your code doesn't compile, so the code cannot execute, and therefore no default constructor is called.

Last edited by 7stud; 02-23-2007 at 08:15 PM.
 
Old 02-23-2007, 08:13 PM   #5
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
Yes, it works now. Thank you.
 
  


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
basic question soldan Linux - General 7 04-13-2006 06:38 AM
A Basic Question abw Linux - Hardware 2 10-06-2004 04:40 PM
basic question jaina2 Linux - Newbie 1 07-26-2003 03:44 AM
Very basic question rajanr Linux - Newbie 2 07-23-2003 04:13 AM
basic question kurgan70 Linux - General 2 12-06-2001 04:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:19 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration