Include Issues

This is a discussion on Include Issues within the C++ Programming forums, part of the General Programming Boards category; I'm recieving strange errors that I think are being caused by how I've included my files. If this is a ...

  1. #1
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Question Include Issues

    I'm recieving strange errors that I think are being caused by how I've included my files.
    If this is a incorrect way to structure my include files then please tell me a correct method.

    there are three files:

    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <cstring>
    
    using std::cout;
    using std::cin;
    using std::string;
    using std::endl;
    using std::ifstream;
    using std::ofstream;
    using std::vector;
    #include "decryptor.h"
    
    using namespace std;
    
    int main()
    {
        cout << "Hello world!" << endl;
        return 0;
    }
    Code:
    class decryptor
    {
        public:
            decryptor();
            ~decryptor(){}
            void getTripNum(char*);
            void fillFromFile();
            void tryPassKey();
            int findSamuel(); //returns 1 for true and zero if false
    
        private:
            vector<int> tripNumInts;
            int currentPasskey;
            string output;
    };
    #include "decryptor.cpp"
    Code:
    decryptor::decryptor()
    {
        currentPasskey = 0;
        output = '\0';
    }
    
    void decryptor::fillFromFile()
    {
        char* fileName = "Xencryption.xe";
        char* temp = 0;
        char* rawTriple = "";
        int count = 0;
        ifstream fin(fileName);
    
        while (!fin.eof())
        {
            while(count < 3)//while we don't have a threesome of numbers; continue
            {
                fin.getline(temp,5,'.');
    
                if (temp) //if We got a valid number
                {
                    strcat(temp,"."); //add back in the period
                    strcat(rawTriple,(const char*)temp);//now add it to rawTriple
                    count++; //and let us know how many numbers rawTriple Has
                }//if
            }// while
    
            getTripNum(rawTriple);
            count = 0; //for starting over
            rawTriple = "";
    
        }//while
    
        return;
    }//function()
    
    //data comes to this function looking like this: 234.51.-234.
    void decryptor::getTripNum(char* rawData)
    {
        char* oneNum;
        char* twoNum;
        char* thrNum;
        int finalNum = 0;
    
        oneNum = strtok(rawData, "."); //snag first number
        twoNum = strtok(rawData, "."); //snag second number
        thrNum = strtok(rawData, "."); //snag third, as if that wasn't obvious...
    
        finalNum += atoi(oneNum);//add all together
        finalNum += atoi(twoNum);
        finalNum += atoi(thrNum);
    
        tripNumInts.push_back(finalNum); //and save it
    
        return;
    }//function()
    
    void decryptor::tryPassKey()
    {
        while (!findSamuel()) //does this Passkey work?
        {
            unsigned int charIter = 0;
            int testch = 0;
    
            while (charIter < tripNumInts.size()) //converts
            {
                testch = (tripNumInts[charIter] - currentPasskey);
                if (testch > 255 || testch <0)
                {
                    output = "";
                    if (testch > 255)
                    {
                        output = "failed to find Samuel in all possible Ranges"; //since we've been increased the passkey slowly we must have failed : (
                        return;
                    }
                    else
                        currentPasskey += testch; //bring passkey in range
                }//if
                output += (char)testch; //add to decrypted output
            }//while
            currentPasskey++; //try next one
        }//while
    
        return;
    }//function()
    
    
    int decryptor::findSamuel()
    {
        unsigned int Iterator = 0;
    
        while(Iterator < tripNumInts.size())  //loop while we don't have 'S'
        {
            if( ((int) 'S') == (tripNumInts[Iterator] - currentPasskey) )//if we found 'S'then check for the rest
            {
                Iterator++;
    
                if( ((int) 'a') == (tripNumInts[Iterator] - currentPasskey) )//if there is an 'a' after it
                {
                    Iterator++;
    
                    if( ((int) 'm') == (tripNumInts[Iterator] - currentPasskey) )
                    return 1; //found Sam
                }//if
            }//if
    
            Iterator++;
        }//while
        return 0; //nope no sam :-(
    }//function

    here are the errors:

    Code:
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|1|error: `decryptor' has not been declared|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|2|error: ISO C++ forbids declaration of `decryptor' with no type|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp||In function `int decryptor()':|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|3|error: `currentPasskey' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|4|error: `output' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|3|warning: unused variable 'currentPasskey'|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|4|warning: unused variable 'output'|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|7|error: `decryptor' is not a class or namespace|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp||In function `void fillFromFile()':|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|13|error: `ifstream' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|13|error: expected `;' before "fin"|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|15|error: `fin' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|23|error: `strcat' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|29|error: `getTripNum' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|29|warning: unused variable 'getTripNum'|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|9|warning: unused variable 'fileName'|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|13|warning: unused variable 'ifstream'|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|39|error: `decryptor' is not a class or namespace|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp||In function `void getTripNum(char*)':|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|46|error: `strtok' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|50|error: `atoi' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|54|error: `tripNumInts' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|54|warning: unused variable 'tripNumInts'|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|59|error: `decryptor' is not a class or namespace|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp||In function `void tryPassKey()':|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|61|error: `findSamuel' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|66|error: `tripNumInts' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|68|error: `currentPasskey' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|71|error: `output' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|80|error: `output' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|80|warning: unused variable 'output'|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|82|error: `currentPasskey' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|82|warning: unused variable 'currentPasskey'|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|61|warning: unused variable 'findSamuel'|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|89|error: `decryptor' is not a class or namespace|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp||In function `int findSamuel()':|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|93|error: `tripNumInts' was not declared in this scope|
    C:\Documents and Settings\*****\My Documents\Test programs\crash\WingC\XDecrypt\decryptor.cpp|95|error: `currentPasskey' was not declared in this scope|
    ||=== Build finished: 24 errors, 9 warnings ===|


    Edit: All the errors occurred in the last file

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    Do not include .cpp files. Only include .h files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    After you remove the #include "decryptor.cpp" from the header, you should #include "decryptor.h" in your decryptor.cpp file. You should add header include guards to decryptor.h. You should add decryptor.cpp to your project/makefile/command line to make sure it is compiled along with your main source file.

    You also need to add #include's to your decryptor.h file for things you use in it. Anything you need in there needs to have a #include (or a forward declaration). That means adding <vector> and <string> and specifying the std namespace.

    Finally, you need to add #include's in your cpp file for everything you use in there. That includes <fstream>, <cstring>, <vector>, <string> and possibly others. And of course you should somehow specify the std namespace where appropriate.

    Once you do all that, you should be ok (at least as far as your include issues go). There are other problems with your code that you must deal with once you get it to compile.

  4. #4
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Thank you so much. I've been coding few years now and the includes have been aggrevating to get to work properly. And to think the remedy was a quick post and a google search of include guards.

    It compiled, Now I know there's no syntax errors I can go back to coding.

    On a completely unrelated note, why is it that std::string isn't recognized unless I include iostream in the same file? Is this just an issue related to my compiler?

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Have you tried

    #include <string>

    ?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Quote Originally Posted by Todd Burch View Post
    Have you tried

    #include <string>

    ?
    Isn't <string> just a header containing misc string manipulation functions? It doesn't actualy contain std::string does it?

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Isn't <string> just a header containing misc string manipulation functions? It doesn't actualy contain std::string does it?
    you are referring to
    "#include <cstring>"

    cstring provides string manipulation functions to work with c-strings (char arrays)

    "#include <string>" gives you std::string

    The reason that including <iostream> works, too is that on your particular implementation (compiler/standard library), <iostream> includes <string> for you. Do not rely on this behaviour, though. Always include everything you need, because perhaps another implementation of <iostream> on another platform does not need and does not include <string>.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by A10 View Post
    It doesn't actualy contain std::string does it?
    When in doubt.... look!
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    When in doubt, you also look in documentations. Look up std::string and it will say what header you need to include.
    I assure you that it won't say iostream.

    Oh and, using directives in headers are usually frowned upon. Use them in .cpp files if you want.
    Last edited by Elysia; 04-04-2008 at 06:51 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use this part of dirent.h
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 01-31-2009, 08:51 AM
  2. Weird, cards not outputting correctly
    By Shamino in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2007, 03:26 PM
  3. C++ code in 1999
    By chen1279 in forum C++ Programming
    Replies: 6
    Last Post: 10-04-2006, 11:02 AM
  4. dont konw what to do next.
    By negevy in forum C Programming
    Replies: 29
    Last Post: 09-09-2005, 04:06 PM
  5. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 01:21 PM

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