C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Static vars in C++ Posted by IDK on 23 Sept 2006 at 11:35 AM
This is my first real C++ project, and since I know more OO than C++ I need some help.

class Super{
   static int size;
public:
   static void setSize(int i){ blocksize = i; }
   static void printSize(){ printf("%d\n",blocksize); }

   virtual char operator [](int) = 0;
};

class ChildOne : Super{
}

class ChildTwo : Super{
}

main(){
   Super.setSize(1);
   ChildOne().printSize();
   ChildOne().setSize(2);
   ChildTwo().printSize();
   ChildTwo().setSize(3);
   Super.printSize(1);
}

Desired output:
1
2
3


How can I achive this output?
Report
A working code Posted by Mohammad Rast on 23 Sept 2006 at 7:42 PM
This message was edited by Mohammad Rast at 2006-9-23 19:46:22

Hi
There were some mistakes and problems in your code. I corrected them, and this is the working code (as your desired output), in C++ (you can create classes in c++,not C, then use 'cout' instead of 'printf'). I compiled it in VC++6, as a 'Win32 Console Application'.
If you have some question, I become glad to answer.
Have a good coding!

Code :
#include <iostream.h>

class Super{
   static int blocksize;
public:
   static void setSize(int i){ blocksize = i; }
   static void printSize(){ cout << blocksize << '\n'; }

   virtual char operator [](int) = 0;
};

int Super::blocksize;

class ChildOne : public Super
{
};

class ChildTwo : public Super
{
};

void main() // or main()
{
	Super::setSize(1);
	ChildOne::printSize();
	ChildOne::setSize(2);
	ChildTwo::printSize();
	ChildTwo::setSize(3);
	Super::printSize();
}







Report
Re: A working code Posted by stober on 23 Sept 2006 at 9:00 PM
:
: void main() // or main()
: {


main ALWAYS returns an int and should be declared that way
int main() 


C might allow void main() but c++ will REQUIRE int main().
Report
Re: A working code Posted by bluj91 on 26 Sept 2006 at 5:32 PM
: :
: : void main() // or main()
: : {
:
:
: main ALWAYS returns an int and should be declared that way
:
: int main() 
: 

:
: C might allow void main() but c++ will REQUIRE int main().
:

People should always declare main() as returning int even in C. Some compilers might tolerate the absense of int in the declaration in C.

ie.
main()
{
}

but most will not tolerate people putting stuff other than an integer as the return type for main
Report
Re: A working code Posted by IDK on 23 Sept 2006 at 11:40 PM

: There were some mistakes and problems in your code. I corrected them, and this is the working code (as your desired output), in C++ (you can create classes in c++,not C, then use 'cout' instead of 'printf'). I compiled it in VC++6, as a 'Win32 Console Application'.
: If you have some question, I become glad to answer.
: Have a good coding!
:
: Code :
:
: #include <iostream.h>
: 
: class Super{
:    static int blocksize;
: public:
:    static void setSize(int i){ blocksize = i; }
:    static void printSize(){ cout << blocksize << '\n'; }
: 
:    virtual char operator [](int) = 0;
: };
: 
: int Super::blocksize;
: 
: class ChildOne : public Super
: {
: };
: 
: class ChildTwo : public Super
: {
: };
: 
: void main() // or main()
: {
: 	Super::setSize(1);
: 	ChildOne::printSize();
: 	ChildOne::setSize(2);
: 	ChildTwo::printSize();
: 	ChildTwo::setSize(3);
: 	Super::printSize();
: }
: 

:
:

I can't even get your code to compile.
Youre using #include <iostream.h> wich is old. Use the new standard.

Also, why did you declare size outside the class again? For me it made no difference.

Anyway I made some progress on my own yesterday and now I only have one error left.

class Super{
   static int size;
public:
   static void setSize(int i){ blocksize = i; }
   static void printSize(){ printf("%d\n",blocksize); }

   virtual char operator [](int) = 0;
};

class ChildOne : Super{
}

class ChildTwo : Super{
}

main(){
   Super::.setSize(1);
   ChildOne::printSize();
   ChildOne::setSize(2);
   ChildTwo::printSize();
   ChildTwo::setSize(3);
   Super::printSize(1);
}


This is my make output:
g++ -c test.cpp -o test.o
block.h: In function 'int main()':
block.h:9: error: 'static void Super::setSize(int)' is inaccessible
test.cpp:5: error: within this context
block.h:10: error: 'static void Super::printSize()' is inaccessible
test.cpp:6: error: within this context
block.h:9: error: 'static void Super::setSize(int)' is inaccessible
test.cpp:7: error: within this context
block.h:10: error: 'static void Super::printSize()' is inaccessible
test.cpp:8: error: within this context
make: *** [test.o] Error 1


How do I make it accessesable?
Report
Re: A working code Posted by Gregry2 on 24 Sept 2006 at 12:13 AM
: I can't even get your code to compile.
: Youre using #include <iostream.h> wich is old. Use the new standard.
:
: Also, why did you declare size outside the class again? For me it made no difference.
:
: Anyway I made some progress on my own yesterday and now I only have one error left.
:
:
: class Super{
:    static int size;
: public:
:    static void setSize(int i){ blocksize = i; }
:    static void printSize(){ printf("%d\n",blocksize); }
: 
:    virtual char operator [](int) = 0;
: };
: 
: class ChildOne : Super{
: }
: 
: class ChildTwo : Super{
: }
: 
: main(){
:    Super::.setSize(1);
:    ChildOne::printSize();
:    ChildOne::setSize(2);
:    ChildTwo::printSize();
:    ChildTwo::setSize(3);
:    Super::printSize(1);
: }
: 

:
: This is my make output:
:
: g++ -c test.cpp -o test.o
: block.h: In function 'int main()':
: block.h:9: error: 'static void Super::setSize(int)' is inaccessible
: test.cpp:5: error: within this context
: block.h:10: error: 'static void Super::printSize()' is inaccessible
: test.cpp:6: error: within this context
: block.h:9: error: 'static void Super::setSize(int)' is inaccessible
: test.cpp:7: error: within this context
: block.h:10: error: 'static void Super::printSize()' is inaccessible
: test.cpp:8: error: within this context
: make: *** [test.o] Error 1
: 

:
: How do I make it accessesable?
:

Is the posted code your actual code? The class definitions aren't ended with a ';'. It could make the compiler see all of the rest of the code out of context

or may be not...sorry, I don't use C++ y'know...
{2}rIng
Report
Re: A working code Posted by stober on 24 Sept 2006 at 4:53 AM
:
: : There were some mistakes and problems in your code. I corrected them, and this is the working code (as your desired output), in C++ (you can create classes in c++,not C, then use 'cout' instead of 'printf'). I compiled it in VC++6, as a 'Win32 Console Application'.
: : If you have some question, I become glad to answer.
: : Have a good coding!
: :
: : Code :
: :
: : #include <iostream.h>
: : 
: : class Super{
: :    static int blocksize;
: : public:
: :    static void setSize(int i){ blocksize = i; }
: :    static void printSize(){ cout << blocksize << '\n'; }
: : 
: :    virtual char operator [](int) = 0;
: : };
: : 
: : int Super::blocksize;
: : 
: : class ChildOne : public Super
: : {
: : };
: : 
: : class ChildTwo : public Super
: : {
: : };
: : 
: : void main() // or main()
: : {
: : 	Super::setSize(1);
: : 	ChildOne::printSize();
: : 	ChildOne::setSize(2);
: : 	ChildTwo::printSize();
: : 	ChildTwo::setSize(3);
: : 	Super::printSize();
: : }
: : 

: :
: :
:
: I can't even get your code to compile.
: Youre using #include <iostream.h> wich is old. Use the new standard.
:
: Also, why did you declare size outside the class again? For me it made no difference.
:
: Anyway I made some progress on my own yesterday and now I only have one error left.
:
:
: class Super{
:    static int size;
: public:
:    static void setSize(int i){ blocksize = i; }
:    static void printSize(){ printf("%d\n",blocksize); }
: 
:    virtual char operator [](int) = 0;
: };
: 
: class ChildOne : Super{
: }
: 
: class ChildTwo : Super{
: }
: 
: main(){
:    Super::.setSize(1);
:    ChildOne::printSize();
:    ChildOne::setSize(2);
:    ChildTwo::printSize();
:    ChildTwo::setSize(3);
:    Super::printSize(1);
: }
: 

:
: This is my make output:
:
: g++ -c test.cpp -o test.o
: block.h: In function 'int main()':
: block.h:9: error: 'static void Super::setSize(int)' is inaccessible
: test.cpp:5: error: within this context
: block.h:10: error: 'static void Super::printSize()' is inaccessible
: test.cpp:6: error: within this context
: block.h:9: error: 'static void Super::setSize(int)' is inaccessible
: test.cpp:7: error: within this context
: block.h:10: error: 'static void Super::printSize()' is inaccessible
: test.cpp:8: error: within this context
: make: *** [test.o] Error 1
: 

:
: How do I make it accessesable?
:


your ocmpiler probably gave you those errors because the class contains pure virtual function (operator [] ), and a class that contains pure virtual functions cannot be instantiated by themselves, and the pure virtual functions MUST be overridden in some derived class.
Report
Re: A working code Posted by IDK on 24 Sept 2006 at 8:52 AM
: :
: : : There were some mistakes and problems in your code. I corrected them, and this is the working code (as your desired output), in C++ (you can create classes in c++,not C, then use 'cout' instead of 'printf'). I compiled it in VC++6, as a 'Win32 Console Application'.
: : : If you have some question, I become glad to answer.
: : : Have a good coding!
: : :
: : : Code :
: : :
: : : #include <iostream.h>
: : : 
: : : class Super{
: : :    static int blocksize;
: : : public:
: : :    static void setSize(int i){ blocksize = i; }
: : :    static void printSize(){ cout << blocksize << '\n'; }
: : : 
: : :    virtual char operator [](int) = 0;
: : : };
: : : 
: : : int Super::blocksize;
: : : 
: : : class ChildOne : public Super
: : : {
: : : };
: : : 
: : : class ChildTwo : public Super
: : : {
: : : };
: : : 
: : : void main() // or main()
: : : {
: : : 	Super::setSize(1);
: : : 	ChildOne::printSize();
: : : 	ChildOne::setSize(2);
: : : 	ChildTwo::printSize();
: : : 	ChildTwo::setSize(3);
: : : 	Super::printSize();
: : : }
: : : 

: : :
: : :
: :
: : I can't even get your code to compile.
: : Youre using #include <iostream.h> wich is old. Use the new standard.
: :
: : Also, why did you declare size outside the class again? For me it made no difference.
: :
: : Anyway I made some progress on my own yesterday and now I only have one error left.
: :
: :
: : class Super{
: :    static int size;
: : public:
: :    static void setSize(int i){ blocksize = i; }
: :    static void printSize(){ printf("%d\n",blocksize); }
: : 
: :    virtual char operator [](int) = 0;
: : };
: : 
: : class ChildOne : Super{
: : }
: : 
: : class ChildTwo : Super{
: : }
: : 
: : main(){
: :    Super::.setSize(1);
: :    ChildOne::printSize();
: :    ChildOne::setSize(2);
: :    ChildTwo::printSize();
: :    ChildTwo::setSize(3);
: :    Super::printSize(1);
: : }
: : 

: :
: : This is my make output:
: :
: : g++ -c test.cpp -o test.o
: : block.h: In function 'int main()':
: : block.h:9: error: 'static void Super::setSize(int)' is inaccessible
: : test.cpp:5: error: within this context
: : block.h:10: error: 'static void Super::printSize()' is inaccessible
: : test.cpp:6: error: within this context
: : block.h:9: error: 'static void Super::setSize(int)' is inaccessible
: : test.cpp:7: error: within this context
: : block.h:10: error: 'static void Super::printSize()' is inaccessible
: : test.cpp:8: error: within this context
: : make: *** [test.o] Error 1
: : 

: :
: : How do I make it accessesable?
: :
:
:
: your ocmpiler probably gave you those errors because the class contains pure virtual function (operator [] ), and a class that contains pure virtual functions cannot be instantiated by themselves, and the pure virtual functions MUST be overridden in some derived class.
:

OK, this will answear both stober's and gregry's Qs.

It isn't the real code, but a simplification.

I've already implemented the operator [] func in both classes. ({} )

The real project is about 10 files...

But the problem is that I still get that the func is inaccessible, it's not anything with the virtual class.

And what did the int Super::blocksize; line do?
It didn't make any difference.

As I said, I know more OO than C++
Report
Re: A working code Posted by stober on 24 Sept 2006 at 11:39 AM
you need to post some real code -- mainly the .h file. It appears that you derived class may be attempting to do something illegal. But its impossible to know what is going on unless we can see your actual classes.

http://groups.google.com/group/gnu.g++.bug/browse_thread/thread/aca9ed4e44b4e3b6/6150922f40dd298a%236150922f40dd298a
Report
Re: A working code Posted by IDK on 24 Sept 2006 at 1:04 PM
This message was edited by IDK at 2006-9-24 13:14:6

: you need to post some real code -- mainly the .h file. It appears that you derived class may be attempting to do something illegal. But its impossible to know what is going on unless we can see your actual classes.
:

Sure, here it is:

block.h:
#include "common.h"

class DataBlock{
   static int blocksize;
public:
   static void setBlocksize(int i){ blocksize = i; }
   static void printSize(){ printf("%d\n",blocksize); }
   virtual char operator [](int) = 0;
};

int DataBlock::blocksize;

class Ramblock : DataBlock{
   char *data;

public:
   Ramblock();
   ~Ramblock();

   char operator[](int);
};

class Fileblock : DataBlock{
   FILE* file;
public:
   Fileblock(char*);
   ~Fileblock();

   char operator[](int);
   void Readblock(FILE*,int);
};


test.cpp
#include "block.h"

int main(){
   DataBlock::setBlocksize(1024);
   Fileblock::setBlocksize(1);
   Ramblock::printSize();
   Ramblock::setBlocksize(2);
   Fileblock::printSize();
   return 0;
}


This is a test version.
The code is exacly the same.
And the error is still that Fileblock cannot access datablocks funcs from main.

EDIT:
I found a sollution from the link.
I put public in front of the super class (or what it's called):
class Ramblock : public DataBlock{
...


Now I get another problem:
block.h:7: error: 'int DataBlock::blocksize' is private
ramblock.cpp:5: error: within this context


I cannot access access blocksize, even if it's private. It says it's not protected so it shouldn't...




Report
Re: A working code Posted by stober on 24 Sept 2006 at 3:13 PM
make the base class in each derived class public. That fixed the problems with my compiler.
class Ramblock : public DataBlock{
<snip>

class Fileblock : public DataBlock{
<snip>

Report
Re: A working code Posted by IDK on 25 Sept 2006 at 12:02 AM
: make the base class in each derived class public. That fixed the problems with my compiler.
:
: class Ramblock : public DataBlock{
: <snip>
: 
: class Fileblock : public DataBlock{
: <snip>
: 

:

Check my edit I did before...

The problem is now that I cannot access blocksize within the ramblock class.

Here's the code:
Ramblock::Ramblock(){
 if((data = new char[blocksize]) == NULL) per("(m)allocation fault");
}

Report
Re: A working code Posted by stober on 25 Sept 2006 at 4:06 AM
: The problem is now that I cannot access blocksize within the ramblock class.
:
: Here's the code:
:
: Ramblock::Ramblock(){
:  if((data = new char[blocksize]) == NULL) per("(m)allocation fault");
: }
: 

:


so, just change it to protected and you won't have any problems.
Report
Re: A working code Posted by IDK on 25 Sept 2006 at 9:35 AM
: : The problem is now that I cannot access blocksize within the ramblock class.
: :
: : Here's the code:
: :
: : Ramblock::Ramblock(){
: :  if((data = new char[blocksize]) == NULL) per("(m)allocation fault");
: : }
: : 

: :
:
:
: so, just change it to protected and you won't have any problems.
:

OK, so I changed it to:
class Ramblock : protected DataBlock{


But now I can't access it outside of the class.
block.h:10: error: 'static void DataBlock::printSize()' is inaccessible
test.cpp:6: error: within this context


Shouldn't public make it public?
And protected should make it protected, ie, not accessible at all.
Now public makes it public but protected in the class, and protected makes it private.
What does private do?
This is wierd.

EDIT: private does the same as protected.

Do anyone know a good reference to this?
None of my books has it in them and I can't find anything on the net.
Report
Re: A working code Posted by gautam on 25 Sept 2006 at 9:04 PM
: : make the base class in each derived class public. That fixed the problems with my compiler.
: :
: : class Ramblock : public DataBlock{
: : <snip>
: : 
: : class Fileblock : public DataBlock{
: : <snip>
: : 

: :
:
: Check my edit I did before...
:
: The problem is now that I cannot access blocksize within the ramblock class.
:
: Here's the code:
:
: Ramblock::Ramblock(){
:  if((data = new char[blocksize]) == NULL) per("(m)allocation fault");
: }
: 

:

Hi,

It seems like you did everything but didn't understand what you were doing at all. What you were doing was private inheritance which will cause problems if you really want public inheritabce.

Secondly private data is private - meaning even a derived class can't access it, however protected means that derived class can access it and modify it - in effect you derived class must not be getting the data from it - instead you should have a function which gets the static data - like

class Super {
   public :
      static int setsize(int i);
      int GetSize() const { return blocksize; }
   ...

    private :
       static int size;
};

int Super::size = 0;



Lastly why do you need the last line ? This is to resolve the class the variable belongs to because in essence it is in the global area of the executeable. Hence the linker errors if you don't do it.
Report
Re: A working code Posted by IDK on 26 Sept 2006 at 8:47 AM
: Hi,
:
: It seems like you did everything but didn't understand what you were doing at all. What you were doing was private inheritance which will cause problems if you really want public inheritabce.
:

I want private inherence of the variable, and public of the funcs.

: Secondly private data is private - meaning even a derived class can't access it, however protected means that derived class can access it and modify it - in effect you derived class must not be getting the data from it - instead you should have a function which gets the static data - like

I thought it was the other way around. Thanks!

:
:
: class Super {
:    public :
:       static int setsize(int i);
:       int GetSize() const { return blocksize; }
:    ...
: 
:     private :
:        static int size;
: };
: 
: int Super::size = 0;
: 
: 

:
: Lastly why do you need the last line ? This is to resolve the class the variable belongs to because in essence it is in the global area of the executeable. Hence the linker errors if you don't do it.
:

I don't know, it was Mohammad Rast that suggested it.

I don't even know what it is and didn't understand your explanation, could you refrase it or something.
Report
Re: A working code Posted by Lundin on 27 Sept 2006 at 7:00 AM
:
: class Super {
:    public :
:       static int setsize(int i);
:       int GetSize() const { return blocksize; }
:    ...
: 
:     private :
:        static int size;
: };
: 
: int Super::size = 0;
: 
: 

:
: Lastly why do you need the last line ? This is to resolve the class the variable belongs to because in essence it is in the global area of the executeable. Hence the linker errors if you don't do it.
:


The last line is how you initialize static members in C++.
There is no other way to do so. You may get linker errors if you don't do it:

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.11

Report
Re: A working code (continued) Posted by stober on 25 Sept 2006 at 2:21 PM
: : The problem is now that I cannot access blocksize within the ramblock class.
: :
: : Here's the code:
: :
: : Ramblock::Ramblock(){
: : if((data = new char[blocksize]) == NULL) per("(m)allocation fault");
: : }
: :

--------------------------------------------------------------------------------


: :
:
:
: so, just change it to protected and you won't have any problems.
:

OK, so I changed it to:

class Ramblock : protected DataBlock{


--------------------------------------------------------------------------------



But now I can't access it outside of the class.

block.h:10: error: 'static void DataBlock::printSize()' is inaccessible
test.cpp:6: error: within this context


--------------------------------------------------------------------------------



:Shouldn't public make it public?
:And protected should make it protected, ie, not accessible at all.
:Now public makes it public but protected in the class, and protected :makes it private.
:What does private do?
:This is wierd.

:EDIT: private does the same as protected.

:Do anyone know a good reference to this?
:None of my books has it in them and I can't find anything on the net.


No that is not what I meant.
class DataBlock{
protected:
   static int blocksize;
public:
   static void setBlocksize(int i){ blocksize = i; }
   static void printSize(){ printf("%d\n",blocksize); }
   virtual char operator [](int) = 0;
};






Report
Re: A working code (continued) Posted by IDK on 26 Sept 2006 at 8:51 AM
: No that is not what I meant.
:
: class DataBlock{
: protected:
:    static int blocksize;
: public:
:    static void setBlocksize(int i){ blocksize = i; }
:    static void printSize(){ printf("%d\n",blocksize); }
:    virtual char operator [](int) = 0;
: };
: 
: 

:
:

Here's my current code:

class DataBlock{
protected:
   static int blocksize;
public:
   static void setBlocksize(int i){ blocksize = i; }
   static void printSize(){ printf("%d\n",blocksize); }
   virtual char operator [](int) = 0;
};

//int DataBlock::blocksize;

class Ramblock : public DataBlock{
   char *data;

public:
   Ramblock();
   ~Ramblock();

   char operator[](int);
//   void Readblock(FILE*,int);
};

class Fileblock : public DataBlock{
   FILE* file;
public:
   Fileblock(char*);
   ~Fileblock();

   char operator[](int);
   void Readblock(FILE*,int);
};


Now compiling works!!!

The linking is another thing, nothing works.

test.o: In function `DataBlock::setBlocksize(int)':
test.cpp:(.text._ZN9DataBlock12setBlocksizeEi[DataBlock::setBlocksize(int)]+0x7): undefined reference to `DataBlock::blocksize'
test.o: In function `DataBlock::printSize()':
test.cpp:(.text._ZN9DataBlock9printSizeEv[DataBlock::printSize()]+0x7): undefined reference to `DataBlock::blocksize'
ramblock.o: In function `Ramblock::Ramblock()':
ramblock.cpp:(.text+0xe6): undefined reference to `DataBlock::blocksize'
ramblock.o: In function `Ramblock::Ramblock()':
ramblock.cpp:(.text+0x134): undefined reference to `DataBlock::blocksize'

Report
Suggestion... Posted by Mohammad Rast on 25 Sept 2006 at 3:35 AM
Hi IDK,

I think if you learn more about Object Oriented coding[/italic] in C++, and use a good compiler, many of your problems will be solved, without any Q/A!

Have good Coding!
Report
Re: Suggestion... Posted by Lundin on 25 Sept 2006 at 3:44 AM
: Hi IDK,
:
: I think if you learn more about Object Oriented coding[/italic] in C++, and use a good compiler, many of your problems will be solved, without any Q/A!
:
: Have good Coding!
:


No offense, but you are not using a good compiler yourself. As mentioned in earlier posts, void main() is not ANSI/ISO C++ and <iostream.h> has been obsolete for 11 years.


Free C++ compilers for Windows can be found here:

Borland 5.5
http://www.winprog.org/tutorial/bcpp.html

Dev / gcc
http://www.bloodshed.net/dev/devcpp.html
Report
Re: Suggestion... Posted by IDK on 25 Sept 2006 at 9:40 AM
: Hi IDK,
:
: I think if you learn more about Object Oriented coding[/italic] in C++, and use a good compiler, many of your problems will be solved, without any Q/A!
:

That's what I'm trying to do now. Asking Qs are one way to learn.
If you know any good reference to this I will gladly use it, but I cannot find any. All my books have nothing on this, and the net isn't much help either.

And about the compiler, have you ever heard of gcc?
gcc is THE compiler.
Report
Re: Suggestion... Posted by Lundin on 25 Sept 2006 at 10:27 AM
: : Hi IDK,
: :
: : I think if you learn more about Object Oriented coding[/italic] in C++, and use a good compiler, many of your problems will be solved, without any Q/A!
: :
:
: That's what I'm trying to do now. Asking Qs are one way to learn.
: If you know any good reference to this I will gladly use it, but I cannot find any. All my books have nothing on this, and the net isn't much help either.
:
: And about the compiler, have you ever heard of gcc?
: gcc is THE compiler.
:


Yep, gcc is conforming to the standards nicely. And while you wait for the compiler to finish chewing at your code, you can always download and install Borland

Report
Re: Suggestion... Posted by IDK on 25 Sept 2006 at 11:42 AM

:
: Yep, gcc is conforming to the standards nicely. And while you wait for the compiler to finish chewing at your code, you can always download and install Borland
:
:

It's not too bad with make.

Also there's some optimised versions, which I haven't tried, but they should be a lot faster.

Does Borland have a compiler for x86 linux?
Report
Re: Suggestion... Posted by Lundin on 25 Sept 2006 at 11:30 PM
:
: :
: : Yep, gcc is conforming to the standards nicely. And while you wait for the compiler to finish chewing at your code, you can always download and install Borland
: :
: :
:
: It's not too bad with make.
:
: Also there's some optimised versions, which I haven't tried, but they should be a lot faster.
:
: Does Borland have a compiler for x86 linux?
:


I think they have something called Kylix which is supposed to work with both Linux and Windows, if their advertising is true.
Report
Re: Suggestion... Posted by IDK on 26 Sept 2006 at 8:30 AM
: : Does Borland have a compiler for x86 linux?
: :
:
:
: I think they have something called Kylix which is supposed to work with both Linux and Windows, if their advertising is true.
:

No, I think Kylix is just another name for Delphi on Linux...
But I haven't looked it up...
Report
Re: Suggestion... Posted by Gregry2 on 26 Sept 2006 at 8:43 AM
This message was edited by Gregry2 at 2006-9-26 8:44:52

: : : Does Borland have a compiler for x86 linux?
: : :
: :
: :
: : I think they have something called Kylix which is supposed to work with both Linux and Windows, if their advertising is true.
: :
:
: No, I think Kylix is just another name for Delphi on Linux...
: But I haven't looked it up...
:

It a Linux port of both Delphi and C++ Builder...

theres even a wikipedia on it

http://en.wikipedia.org/wiki/Kylix_programming_tool

{2}rIng

EDIT: Wow, it sounds real horrid...

Report
Re: Suggestion... Posted by Lundin on 27 Sept 2006 at 2:43 AM
: This message was edited by Gregry2 at 2006-9-26 8:44:52

: : : : Does Borland have a compiler for x86 linux?
: : : :
: : :
: : :
: : : I think they have something called Kylix which is supposed to work with both Linux and Windows, if their advertising is true.
: : :
: :
: : No, I think Kylix is just another name for Delphi on Linux...
: : But I haven't looked it up...
: :
:
: It a Linux port of both Delphi and C++ Builder...
:
: theres even a wikipedia on it
:
: http://en.wikipedia.org/wiki/Kylix_programming_tool
:
: {2}rIng
:
: EDIT: Wow, it sounds real horrid...
:


When using Borland, Delphi == C++ Builder
Builder is actually running Object Pascal code.

Report
Want Reference ? Posted by Mohammad Rast on 25 Sept 2006 at 7:19 PM
Hi IDK,
I have the 'C++ Primer 3rd'(2.37 MB) E-Book, if you want that, email me and I will send it to your email.
Report
Reply to Lundin Posted by IDK on 27 Sept 2006 at 10:23 AM
Thanks, that solved the problem, but created another:

I'll let gcc speak for it self:
fileblock.o:(.data+0x0): multiple definition of `DataBlock::blocksize'
test.o:(.data+0x0): first defined here
ramblock.o:(.data+0x0): multiple definition of `DataBlock::blocksize'
test.o:(.data+0x0): first defined here


I've included block.h, where I have the class definitions, in fileblock.cpp, ramblock.cpp and test.cpp.


And another thing:
Quote from the link you gave me:
The usual place to define static data members of class Fred is file Fred.cpp (or Fred.C or whatever source file extension you use).


Why should they be placed in the cpp, woulnd't it be better to have them in the header, so one wont forget about declaring it?
Report
Re: Reply to Lundin Posted by Lundin on 28 Sept 2006 at 2:24 AM
: Thanks, that solved the problem, but created another:
:
: I'll let gcc speak for it self:
:
: fileblock.o:(.data+0x0): multiple definition of `DataBlock::blocksize'
: test.o:(.data+0x0): first defined here
: ramblock.o:(.data+0x0): multiple definition of `DataBlock::blocksize'
: test.o:(.data+0x0): first defined here
: 

:
: I've included block.h, where I have the class definitions, in fileblock.cpp, ramblock.cpp and test.cpp.


Would it be possible to post the whole modified code again?


: And another thing:
: Quote from the link you gave me:
: The usual place to define static data members of class Fred is file Fred.cpp (or Fred.C or whatever source file extension you use).
: 

:
: Why should they be placed in the cpp, woulnd't it be better to have them in the header, so one wont forget about declaring it?

I like to think of the h-file as a document provided to an other programmer to show him what to expect from your class. Since private statics are of no interest outside of the class, there is no reason to show them in the h-file.

In C it is even more obvious, you -can't- place variable declarations/initializations in the h-file or you'll be in trouble when more than 1 .c file includes that h-file.


Report
Re: Reply to Lundin Posted by IDK on 28 Sept 2006 at 9:42 AM
: : Thanks, that solved the problem, but created another:
: :
: : I'll let gcc speak for it self:
: :
: : fileblock.o:(.data+0x0): multiple definition of `DataBlock::blocksize'
: : test.o:(.data+0x0): first defined here
: : ramblock.o:(.data+0x0): multiple definition of `DataBlock::blocksize'
: : test.o:(.data+0x0): first defined here
: : 

: :
: : I've included block.h, where I have the class definitions, in fileblock.cpp, ramblock.cpp and test.cpp.
:
:
: Would it be possible to post the whole modified code again?
:

Sure, here's block.h:
What else do I need?
#include "common.h"

class DataBlock{
protected:
   static int blocksize;
public:
   static void setBlocksize(int i){ blocksize = i; }
   static void printSize(){ printf("%d\n",blocksize); }
   virtual char operator [](int) = 0;
};

int DataBlock::blocksize = 1024;

class Ramblock : public DataBlock{
   char *data;

public:
   Ramblock();
   ~Ramblock();

   char operator[](int);
//   void Readblock(FILE*,int);
};

class Fileblock : public DataBlock{
   FILE* file;
public:
   Fileblock(char*);
   ~Fileblock();

   char operator[](int);
   void Readblock(FILE*,int);
};


:
: : And another thing:
: : Quote from the link you gave me:
: : The usual place to define static data members of class Fred is file Fred.cpp (or Fred.C or whatever source file extension you use).
: : 

: :
: : Why should they be placed in the cpp, woulnd't it be better to have them in the header, so one wont forget about declaring it?
:
: I like to think of the h-file as a document provided to an other programmer to show him what to expect from your class. Since private statics are of no interest outside of the class, there is no reason to show them in the h-file.
:
:


Thanks, that cleared up a lot of things, and now I think I know the sollution.

:
: In C it is even more obvious, you -can't- place variable declarations/initializations in the h-file or you'll be in trouble when more than 1 .c file includes that h-file.
:
:


That's the problem I'm facing now.

The sollution would be to have a cpp file with only the declaration, (and maybe the static funcs too).
Report
Re: Reply to Lundin Posted by Lundin on 28 Sept 2006 at 11:51 PM
: : : Thanks, that solved the problem, but created another:
: : :
: : : I'll let gcc speak for it self:
: : :
: : : fileblock.o:(.data+0x0): multiple definition of `DataBlock::blocksize'
: : : test.o:(.data+0x0): first defined here
: : : ramblock.o:(.data+0x0): multiple definition of `DataBlock::blocksize'
: : : test.o:(.data+0x0): first defined here
: : : 

: : :
: : : I've included block.h, where I have the class definitions, in fileblock.cpp, ramblock.cpp and test.cpp.
: :
: :
: : Would it be possible to post the whole modified code again?
: :
:
: Sure, here's block.h:
: What else do I need?
:
: #include "common.h"
: 
: class DataBlock{
: protected:
:    static int blocksize;
: public:
:    static void setBlocksize(int i){ blocksize = i; }
:    static void printSize(){ printf("%d\n",blocksize); }
:    virtual char operator [](int) = 0;
: };
: 
: int DataBlock::blocksize = 1024;
: 
: class Ramblock : public DataBlock{
:    char *data;
: 
: public:
:    Ramblock();
:    ~Ramblock();
: 
:    char operator[](int);
: //   void Readblock(FILE*,int);
: };
: 
: class Fileblock : public DataBlock{
:    FILE* file;
: public:
:    Fileblock(char*);
:    ~Fileblock();
: 
:    char operator[](int);
:    void Readblock(FILE*,int);
: };
: 

:
: :
: : : And another thing:
: : : Quote from the link you gave me:
: : : The usual place to define static data members of class Fred is file Fred.cpp (or Fred.C or whatever source file extension you use).
: : : 

: : :
: : : Why should they be placed in the cpp, woulnd't it be better to have them in the header, so one wont forget about declaring it?
: :
: : I like to think of the h-file as a document provided to an other programmer to show him what to expect from your class. Since private statics are of no interest outside of the class, there is no reason to show them in the h-file.
: :
: :

:
: Thanks, that cleared up a lot of things, and now I think I know the sollution.
:
: :
: : In C it is even more obvious, you -can't- place variable declarations/initializations in the h-file or you'll be in trouble when more than 1 .c file includes that h-file.
: :
: :

:
: That's the problem I'm facing now.
:
: The sollution would be to have a cpp file with only the declaration, (and maybe the static funcs too).
:


I see nothing wrong in that h file. And yes, you should place the static initialization in the cpp file.
Report
Re: Reply to Lundin Posted by IDK on 29 Sept 2006 at 7:55 AM
:
: I see nothing wrong in that h file. And yes, you should place the static initialization in the cpp file.
:

Yes you do...

I've initialated blocksize in the .h file, wich I shouldn't...



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.