Problems with my first class.

This is a discussion on Problems with my first class. within the C++ Programming forums, part of the General Programming Boards category; main.cpp Code: #include <iostream> #include "Memoryx.cpp" int main(){ Memoryx::que(); return 1; } Memoryx.cpp Code: #include "Memoryx.h" Memoryx::que(){ printf("aaa"); } Memoryx.h ...

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

    Problems with my first class.

    main.cpp
    Code:
    #include <iostream>
    
    #include "Memoryx.cpp"
    
    
    int main(){
        Memoryx::que();
        return 1;
    }
    Memoryx.cpp
    Code:
    #include "Memoryx.h"
    
    Memoryx::que(){
             printf("aaa");
    }
    Memoryx.h
    Code:
    #ifndef __MEMORYX_H__
    #define __MEMORYX_H__
    
    class Memoryx{
          public:
               que();
    };
    
    #endif
    Error:
    Code:
    Compiler: Default compiler
    Building Makefile: "C:\Documents and Settings\robdah01\Skrivbord\classtest\Makefile.win"
    Executing  make...
    make.exe -f "C:\Documents and Settings\robdah01\Skrivbord\classtest\Makefile.win" all
    g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"   
    
    In file included from main.cpp:3:
    Memoryx.h:6: error: ISO C++ forbids declaration of `que' with no type
    
    main.cpp: In function `int main()':
    main.cpp:8: error: cannot call member function `int Memoryx::que()' without object
    
    make.exe: *** [main.o] Error 1
    
    Execution terminated
    This is my first class, i get an error while compiling.
    How much i try to figure out why this isn't working.
    I want to learn how to write file inclusions, and classes.
    Last edited by ogelami; 04-13-2010 at 04:18 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Well, one problem is that you gailed to declare and define Memoryx::que with a return type, e.g., void. Another problem is that Memoryx::que is a non-static member function, so it should be called from a Memoryx object. You also should #include <cstdio> in Memoryx.cpp in order to use printf, which should be qualified as std:rintf.

    By the way, names that begin with an underscore followed by an uppercase letter, or that contain consecutive underscores, are reserved to the (compiler and standard library) implementation for any use. Therefore, instead of using __MEMORYX_H__ for the header inclusion guard, use say, MEMORYX_H_.

    Also, the global main function normally returns 0 upon successful execution.
    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

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    6
    Quote Originally Posted by laserlight View Post
    Well, one problem is that you gailed to declare and define Memoryx::que with a return type, e.g., void. Another problem is that Memoryx::que is a non-static member function, so it should be called from a Memoryx object. You also should #include <cstdio> in Memoryx.cpp in order to use printf, which should be qualified as std:rintf.

    By the way, names that begin with an underscore followed by an uppercase letter, or that contain consecutive underscores, are reserved to the (compiler and standard library) implementation for any use. Therefore, instead of using __MEMORYX_H__ for the header inclusion guard, use say, MEMORYX_H_.

    Also, the global main function normally returns 0 upon successful execution.
    New code, but still, it's not working :/

    main.cpp
    Code:
    #include <iostream>
    #include <cstdio>
    #include "Memoryx.cpp"
    
    
    int main(){
        Memoryx::que();
        return 0;
    }
    Memoryx.cpp
    Code:
    #include <cstdio>
    #include "Memoryx.h"
    
    static void Memoryx::que(){
             std::printf("aaa");
    }
    Memoryx.h
    Code:
    #ifndef MEMORYX_H_
    #define MEMORYX_H_
    
    class Memoryx{
          public:
             static void que();
    };
    
    #endif
    Error:
    Code:
    Compiler: Default compiler
    Building Makefile: "C:\Documents and Settings\robdah01\Skrivbord\classtest\Makefile.win"
    Executing  make...
    make.exe -f "C:\Documents and Settings\robdah01\Skrivbord\classtest\Makefile.win" all
    g++.exe -c Memoryx.cpp -o Memoryx.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"   
    
    Memoryx.cpp:4: error: cannot declare member function `static void Memoryx::que()' to have static linkage
    
    make.exe: *** [Memoryx.o] Error 1
    
    Execution terminated

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you should not include cpp file - you include h file

    then - you compile and link both cpp files together
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    6
    Thank you, problem solved!
    Code:

    main.cpp
    Code:
    #include <iostream>
    #include <cstdio>
    #include <conio.h>
    #include "Memoryx.h"
    
    
    int main(){
        Memoryx::que();
        getch();
        return 0;
    }
    Memoryx.cpp
    Code:
    #include <cstdio>
    #include "Memoryx.h"
    
    void Memoryx::que(){
             std::printf("aaa");
    }
    Memoryx.h
    Code:
    #ifndef MEMORYX_H_
    #define MEMORYX_H_
    
    class Memoryx{
          public:
             static void que();
    };
    
    #endif

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You should know that you have no class objects there. Becuase you have only static functions, your class is really nothing more than a namespace. There's not really any OOP going on here.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. class Template problems
    By Mr_roboto in forum C++ Programming
    Replies: 8
    Last Post: 02-16-2006, 10:21 PM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

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