Problems with Header Files in CB

This is a discussion on Problems with Header Files in CB within the C++ Programming forums, part of the General Programming Boards category; Hello, I am just starting to learn about classes and currently am learning about how to declare classes in a ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    34

    Problems with Header Files in CB

    Hello, I am just starting to learn about classes and currently am learning about how to declare classes in a separate file though I am having trouble as I keep getting the error code saying No such file or directory. I am using Code::Blocks and I have tried looking this up though I have not seen anything that has been helpful for me. My program works when it is all in one file though when seperated into the different files it will not work, even when I use the default code::blocks class setup. Here is my different code files. I will say what folder everything is in incase that is what is causing the problems

    In my main.cpp(which is found in my sources folder) I have
    Code:
    #include <iostream>
    #include "Prime_Number_Detector.h"
    using namespace std;
    
    
    int main()
    {
        cout << "Enter a number!" << endl;
        Prime_Number_Detector test;
        long long H;
        cin>> H;
        test.number_input(H);
        cout<<"Testing if prime number.\n";
        bool T;
        T = test.prime_test();
        if(T == true){
            cout<<H << " is a prime number!\n";
        }
        else{
            cout<<H <<" is not a prime number. =(\n";
        }
        return 0;
    }
    In my file Prime_Number_Detector.cpp(Found in the src folder which is in the Sources Folder) I have
    Code:
    
    #include <iostream>
    #include "Prime_Number_Detector.h"
    using namespace std;
    
    
    Prime_Number_Detector::Prime_Number_Detector()
    {
        //ctor
    }
    
    
    Prime_Number_Detector::~Prime_Number_Detector()
    {
        //dtor
    }
    
    
    void Prime_Number_Detector::number_input(long long y){
        number = y;
    }
    
    
    long long Prime_Number_Detector::sqrt_int(){
        long long x;
        long long z;
        for(long long i = 1; z < number; i++){
            z = i*i;
            x = i;
        }
        return x;
    }
    
    
    bool Prime_Number_Detector::prime_test(){
        long long x = sqrt_int();
        long long y = 0;
        bool test = true;
        while(test == true && y <= x){
            if(number % 2 != 0){
                for( y = 3; y <= x; y += 2){
                    if(number%y == 0){
                        test = false;
                    }
                }
            }
            else{
                test = false;
            }
        }
        return test;
    }
    And Finally in my Prime_Number_Detector.h(Found in the include folder which was in the Headers folder) I have

    Code:
    #ifndef PRIME_NUMBER_DETECTOR_H
    #define PRIME_NUMBER_DETECTOR_H
    
    
    
    
    class Prime_Number_Detector
    {
        public:
            Prime_Number_Detector();
            virtual ~Prime_Number_Detector();
            void number_input(long long);
            bool prime_test();
    
    
    
    
        protected:
        private:
            long long number;
            long long sqrt_test;
            long long sqrt_int();
    };
    
    
    #endif // PRIME_NUMBER_DETECTOR_H
    Does anyone have any idea where I might of made a mistake on creating my class files and headers?

    Oh and incase anyone asks I have using long long because I am going to be testing out very large numbers for a problems I want to try, which is to find the largest prime factor of a number.

    Edit: If anyone has a suggestion on another method on calculating if a number is prime it would be greatly appreciated as currently this test will take a long time.
    Last edited by thadis_4; 05-09-2012 at 11:09 PM. Reason: Adding additional question

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    (The code looks okay, without looking minutely)

    Make a project in C::B instead of just files, and when adding a new .cpp or .h file, choose the "Add to current project" or something similar option.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    My suggestion is that you save in your class a vector of previously found primes, then check them first against the test value. Also remember that you don't need to test it with primes greater than its square root.

    What is "sqrt_int()" for? [edit]( Never mind, I found it )[/edit]
    Last edited by GReaper; 05-09-2012 at 11:44 PM.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    manasij7479:
    I thought I added an option that said add to current project though I might of either misremembered or what I clicked just did not do what I expected.

    GReaper:
    I thought about something like using a list of found primes though I did not know how I would use it as I am not extremely familiar with vectors. And yes the sqrt_int(); is used for finding a number greater than the actual square root saying I was going to try to avoid using float points and couldn't remember if you have a number like 5.3 if it would round to 6 or 5.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Floating point numbers are always truncated when they're converted to integers. That said, I'm not sure whether they round towards zero or towards -INF.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Yeah I just decided to use my own version of square root to do the part of the built in one though I guess now that I think of it I could of just done the C++ one +1. Also I tried adding my two files for my header(.h and the .cpp) by right clicking on the project and saying "Add File" and add the two files and it still will not work. Any other ideas that might be causing my problems?

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Have you added all the files to the same build? Code::Blocks gives two default builds, Debug & Release.
    Devoted my life to programming...

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    If you want to get anywhere near checking numbers round 2^64-1, then you'll need a better test such as http://en.wikipedia.org/wiki/Miller%...primality_test.

    I'd also like to say that this is a bad use of classes. You wont learn how to correctly use them, or understand why they are used, if you use them for inappropriate purposes. Testing for primeness screams of just writing a function that takes a single input and produces a single output, and has no side-effects.
    You might learn about prime number determination, but it simply isn't a good idea to combine that with learning how to use classes.
    Would you like a suggestion of what would be good to implement to try and learn about classes?
    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"

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    I believe I added both the .cpp and the .h to the project in both the release and debug though I will check it when I get home.

    IMalc:
    What would you suggest as a class? I only did it this way because i was learning how to create classes and wanted to try to create the header files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  2. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  3. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 03:50 AM
  4. problems with header files
    By robid1 in forum C++ Programming
    Replies: 15
    Last Post: 08-16-2004, 07:35 AM
  5. header files and code files..
    By CompiledMonkey in forum C++ Programming
    Replies: 4
    Last Post: 02-15-2002, 09:35 AM

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