frustrated c noob

This is a discussion on frustrated c noob within the C++ Programming forums, part of the General Programming Boards category; I am an experienced programmer using pascal with borlands delphi IDE and have recently took it upon myself to start ...

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

    frustrated c noob

    I am an experienced programmer using pascal with borlands delphi IDE and have recently took it upon myself to start to learn C. I have so far been able to figure out how to do things using google and some books. I am however at a loss for the following.

    In delphi I could easily create a custom class using the class keyword and despite seaching for custom class and object related tutorials and exmpales on the internet I still have no idea how to wite a custom class. The following is a small test example I am trying to get working:

    Code:
    #include <stdio.h>
    
    class MyClass
    {
    public:
       MyClass(int num)
       {
          setNum(num);
       }
    
       void setNum(int num)
       {
          number = num;
       }
    
       int getNum()
       {
          return number;
       }
    private:
       int number;
    };
    
    main()
    {
            int a;
            MyClass myc1(3);
    
            a = myc1.getNum();
    
            printf("the number is %d\n", a);
    
    }
    when I try to compile it using gcc I get:

    tmp.c:4: parse error before "MyClass"
    tmp.c:4: warning: data definition has no type or storage class
    tmp.c:6: parse error before "MyClass"
    tmp.c:7: syntax error before '{' token
    tmp.c: In function `setNum':
    tmp.c:16: `number' undeclared (first use in this function)
    tmp.c:16: (Each undeclared identifier is reported only once
    tmp.c:16: for each function it appears in.)
    tmp.c: In function `getNum':
    tmp.c:21: `number' undeclared (first use in this function)
    tmp.c: At top level:
    tmp.c:23: parse error before ':' token
    tmp.c: In function `main':
    tmp.c:30: parse error before "myc1"
    tmp.c:32: `myc1' undeclared (first use in this function)
    I guess this may be a trivial thing for most c programmers but any help would be greatly appreciated and more importantly lower my blood pressure. . Thanks

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You're trying to write C++! C doesn't have classes. The closest would be a struct.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    Quote Originally Posted by QuantumPete View Post
    You're trying to write C++! C doesn't have classes. The closest would be a struct.
    Even so, a struct doesn't contain functions. (Pointer to functions at best, but that's for another day).
    Anyway, this code is valid, if you use C++. Rename your file .cpp and feed it to gcc (or g++, more accurately). You might want to give a return type to your main function though, preferably int.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    5
    Thanks guys will do. Thats quite interesting to find out.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Always return int for main, whether that be C or C++:
    http://cpwiki.sourceforge.net/Implicit_main

    Btw, C++ uses cout instead of printf.
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Btw, C++ uses cout instead of printf.
    Assuming we wish to do so, C++ is perfectly fine to use printf, scanf etc. The include for that would be <cstdio> instead of <stdio.h>, however.

    It is of course more "C++" to use cout and the other iostream functions, but it's perfectly possible, and in no way incorrect, to use printf in C++.

    Actually, there are some formatting, for example tables, that is quite a bit easier to do in printf than cout. You should of course not mix cout and stdio type IO functions - that would lead to undefined behaviour.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    You should of course not mix cout and stdio type IO functions - that would lead to undefined behaviour.
    From what I understand, that is not the case. If so, then std::ios::sync_with_stdio() would not make sense.

    Oh, and moved to the C++ programming forum, in view that dodo10 may actually want to learn C++ instead of C.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    From what I understand, that is not the case. If so, then std::ios::sync_with_stdio() would not make sense.

    Oh, and moved to the C++ programming forum, in view that dodo10 may actually want to learn C++ instead of C.
    Ah, I should have done a more thorough explanation: what I actually meant, you can't arbitrarily switch - you do need to make sure they are both in sync with each other (and I could not remember the name of the function, but yes it is POSSIBLE - just not the ideal thing - to mix the two).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob in need of help.
    By gec5741 in forum C++ Programming
    Replies: 18
    Last Post: 01-23-2009, 03:25 PM
  2. Very frustrated and tired of asking for help
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 11-17-2008, 07:29 AM
  3. I'm a noob and I need help.
    By nifear4 in forum C Programming
    Replies: 17
    Last Post: 10-14-2008, 02:20 PM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 09:49 AM
  5. so confused and frustrated
    By ct26torr in forum C Programming
    Replies: 2
    Last Post: 02-13-2003, 10:40 PM

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