Thread: Declared Error

  1. #1
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63

    Declared Error

    Im trying out using headers, and it seems that my program cant find anything in it.
    Code:
    //in filecheck.cpp
    if (check("test.txt")) {
        [does this]
    }
    
    //in check.h
    int check(const char *filename) {
    	FILE* fp;
    	if ((fp = fopen( filename, "r")) != NULL) {
    		fclose(fp); 
    		return 1; 
    	}
    	return 0;
    }
    Every time I try to use a function in my header, my compiler just says that its undeclared and wont compile. Accually, it says nothing in my header is undeclared. Am I supposed to use something special in my file other then ' #include"check.h" ' or what?
    Last edited by LloydUzari; 08-06-2004 at 08:03 PM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    //test.h
    int check(const char *);
     
    //in test.cpp
    int check(const char * filename)
    {
      ifstream fin(filename);
      if(!fin)
    	cout << "unable to open file" << endl;
      else
    	cout << "file open" << endl;
     
      return 0;
    }

  3. #3
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Thanks, that worked, but I was trying to put function in the header, and it seems I forgot the 'n' in #ifndef
    Now I have one more error. I tried adding a load function into the header, and now it says:
    undefined reference to 'load(char const*)'
    Now whats wrong?

    Code:
    //in test.cpp, nothing special
    load("test.txt");
    
    //in test.h
    void load(char *filename) {
        [does this]
    }

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    headers in C are for prototypes (what has been explained above)
    in your second example, the prototype shoule be in the header and the implementation in the cpp file (just like the first example).

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Also, you are passing a const char* to a function that takes a char*.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM