Thread: Problems with Header Files in CB

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 10:09 PM. Reason: Adding additional question

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, 02:50 AM
  4. problems with header files
    By robid1 in forum C++ Programming
    Replies: 15
    Last Post: 08-16-2004, 06:35 AM
  5. header files and code files..
    By CompiledMonkey in forum C++ Programming
    Replies: 4
    Last Post: 02-15-2002, 09:35 AM