Thread: interface, implementation, & application files

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    25

    interface, implementation, & application files

    Hi all,
    This program works well. The the object of data type of class used to count something.
    However, the compiler generated the error when I broke this program into 3 parts:
    - The interface file, Problem5.h, contains the class definition.
    - The implementation file, Problem5.cpp, contains the class's member functions, #include "Problem5.h" was written.
    - The application file used to test above class, #include "Problem5.h" was written.

    * When I compiled them, it was fine. However, when I ran them, it seemed to get trouble with the linker.
    The weird thing is, the program works perfectly when I broke the program into 2 parts, the file contains the definiton of class and its member functions' definition, called Problem5.h; and the file contains the main() function.
    Everyone can help me out please. Here is the code:

    Code:
    #include <iostream>
    using namespace std;
    
    class CounterType
    {
    public:
      CounterType();//Default Constructor
    
      CounterType(int number); //Constructor
      
    
      void increase();
      
    
      void decrease();
      
    
      int get();
      
      void output(ostream& numberout);
      
    
    private:
      int count;
      void check();
    
    };
    
    // <---- interface file
    int main()
    {
    	CounterType myclass;
    	CounterType fortest(100);
    	int countvalue,i;
    	for (i=0;i<10;i++)
    	{
    		myclass.increase();
    		if ((i==3)||(i==7))
    			myclass.decrease();
    	}
    	countvalue=fortest.get();
    	cout<<"The first object that counts from 1-->10, except 3&7 :";
    	myclass.output(cout);
    	cout<<endl<<endl;
    	cout<<"The second object that is initilized with 100: "<<countvalue<<endl<<endl;
    
    	return 0;
    }
    //<---- application (drive) file
    
    CounterType::CounterType() : count(0)
    {
    }
    
    CounterType::CounterType(int number)
    {
    	count=number;
    	check();
    }
    
    void CounterType::check()
    {
    	if (count<0)
    	{
    		cout<<"Count contains negative number. No negative number is allowed!\n\n";
    		exit(1);
    	}
    }
    
    void CounterType::increase()
    {
      count++;
      check();
    }
    
    void CounterType::decrease()
    {
      if (count > 0)
        count--;
    	check();
    }
    
    int CounterType::get()
    {
      return count;
    }
    
    void CounterType::output(ostream& numberout)
    {
      numberout << count;
    }
    //<----- implementation file
    Last edited by trongsi; 04-08-2006 at 08:57 AM.

  2. #2
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    You might try enclosing the whole header file with preprocessor protection guards against multiple inclusion otherwise you might have problems with multiple declarations. Try:
    Code:
    //problem5.h
    #ifndef _PROBLEM5_H_
    #define _PROBLEM5_H_
    
    //all your previous contents of the problem5.h go in here
    
    #endif
    Not sure if this is the problem but it's worth a try...

    Cheers

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Well, what's the error? Or, what's the code?

    If you're really stuck, try post the three files so we can have a look at them.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    25
    Thanks for your helps.
    I've just figured out the problem.
    Since I have nearly no experience with C++, I compiled the implementation file & the app file separately, then opened the app file to run, of course, it didn't work.
    One guy gave me a hint that I must open 3 files at the same work space, then compile one by one file (except interface.h file), then run the program. Now, It works.
    To LinuxCoder,
    the flag PROBLEM5_H followed #ifndef tells the compiler to skip the class definition if it is already defined. I included those code already, and it was not this problem.
    Anyway, thank you alll. Have a great weekend.
    Last edited by trongsi; 04-08-2006 at 01:58 PM.

  5. #5
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    the flag PROBLEM5_H followed #ifndef tells the compiler to skip the class definition if it is already defined.
    It's not a flag, from what i've learned it's called a SYMBOL and it's used by #ifndef which is a preprocessor command, these will be evaluated and executed before the actual compiling process starts (so the name preprocessor).

    Anyway i'm just being picky about it. I'm really glad you got your problem solved. As you can imagine without details on the compiler errors i was left in the dark about what these errors could be, so it was nothing but a guess.

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  2. Running the application (.exe) files on other PCs
    By csonx_p in forum Windows Programming
    Replies: 11
    Last Post: 06-11-2008, 09:31 PM
  3. multilayer perceptron application
    By ritchie888 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2008, 02:17 PM
  4. Replies: 2
    Last Post: 02-11-2002, 01:03 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM