Thread: Problems with my first class.

  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 03:18 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should not include cpp file - you include h file

    then - you compile and link both cpp files together
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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,318
    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, 01: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