Why this code is not working?

This is a discussion on Why this code is not working? within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> using namespace std; class man; class car{ public: car(){ } }; class man{ public: car c; man() { ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    Why this code is not working?

    Code:
    #include<iostream>
    
    using namespace std;
    
    class man;
    
    class car{
            public:
            car(){
            }
    };
    
    class man{
            public:
            car c;
            man()   {
            }
    };
    
    int main()      {
            return (0);
    }
    Giving compile time error!

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    *What* compile time error?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    134

    Strange

    Strange, It was throwing error, I restarted my computer, its fine now, how that could happen?
    and is this code OK? or my programming style is bad?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by kapil1089thekin View Post
    Strange, It was throwing error, I restarted my computer, its fine now, how that could happen?
    and is this code OK? or my programming style is bad?
    Probably there was something complety else wrong. Maybe you didn't safe the file until you rebooted your computer or something simple like that. About your coding style:
    * "#include<iostream>" I prefer a space before the "<"
    * "class man;" is completely useless: remove that line.
    * The empty constructors are completely useless: remove them.
    * "public:" indented is not very clear, in my opinion. You should probably remove the tab/spaces in front of it.
    * class names should usually start with an uppercase, like Man and Car.
    * This way, a man has exactly one car: never more, never less. Maybe that's fine for your example though, but in words it doesn't make much sense.
    * The number of spaces before the opening "{" isn't consistent. Use one, or do it on a newline.
    * Usually, you should split classes over multiple header/source files.
    * I personally dislike "return (0);". It's like people think it's a function. "return 0;" makes more sense.

    Other than that, it's fine ;-).

  5. #5
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56
    Probably your program was already running while you was trying to run it again, then when you restarted it worked...

    Was the error something like "bla bla returned with exit code -1"? It happens to me sometimes.
    2B OR !2B? That is the question!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Quote Originally Posted by EVOEx View Post
    * class names should usually start with an uppercase, like Man and Car.
    Hmmm... no, they should start with a capital C!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Elysia View Post
    Hmmm... no, they should start with a capital C!
    Well, lucky for him, half of them already do. So that would make it Car and CMan? ;-)

    I think you're being sarcastic, but I hate that "C-prefix-naming-convention". I use the same naming conventions for classes and structs: one capital per word.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    No, it should be CCar and CMan, and I wasn't being sarcastic, actually.
    If you suggest using capital letters, I suggest using a C prefix.
    Point is, this is entirely a matter of style, so there's no right or wrong. You can't say anyone should use capital letters for types. If you can that, then I can say they should be prefixed with a C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Why should we start classes with a 'C'? Whats the benefit?
    What if we define types starting with a capital, objects with lower case, constants all capital and functions like types.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    If you want to do
    CCar Car;

    I never reserve myself to create objects starting with lower case.
    I hate camel case. And for others who do, the "C" prefix is helpful.
    I append _t to typedefs, as well. I prepend S to structs. I suppose you could just add T or prefix or postfix something else, but that's up to you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Quote Originally Posted by Elysia View Post
    If you want to do
    CCar Car;
    Why should anyone do that? It's like changing int to tint so we can have tint int;

    I read somewhere the reason MFC uses that style is because at the time MFC was been implemented namespaces were not implemented.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Why? Because some people prefer it. I do, for example.
    It's a style choice! Nothing wrong with that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    It's a step above/below Hungarian notation is what it is.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Absolute nonsense.
    Is differentiating member variables from local variables hungarian, too?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    I don't get why you're so mad about a simple fact. To address your question, no, you can differentiate from members and locals without resorting to one-letter type-related prefixes. Don't be mad that your Hungarian like approach is unpopular. C++'s type system makes using it extraneous. That's all -- in other places where types are dynamic it's more useful, as you probably know.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 02:57 AM
  2. Replies: 3
    Last Post: 02-24-2009, 08:49 PM
  3. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 05:13 PM
  4. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 08:47 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 12:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21