Thread: trouble with .h and .cpp files

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    trouble with .h and .cpp files

    I'm new to C++ and am having trouble implementing this extremely simple class in Visual C++:

    The file testClass.h contains:

    Code:
    #ifndef TESTCLASS_H
    #define TESTCLASS_H
    
    class testClass
    {
    public:
    	testClass(void);
    	~testClass(void);
    	vector<int> v;
    };
    
    testClass::testClass(void)
    {
    }
    
    testClass::~testClass(void)
    {
    }
    #endif


    (Vector is #included in main.) That's all very simple and it compiles properly. However if I take the constructor and destructor and move them to a .cpp file, as everyone says you're supposed to do, it breaks:

    file testClass.h
    Code:
    #ifndef TESTCLASS_H
    #define TESTCLASS_H
    
    class testClass
    {
    public:
    	testClass(void);
    	~testClass(void);
    	vector<int> v;
    };
    #endif
    with file testClass.cpp:
    Code:
    #include "testClass.h"
    
    testClass::testClass(void)
    {
    }
    
    testClass::~testClass(void)
    {
    }
    After splitting the code into two files, the compiler complain about the vector<int> v; line.

    syntax error : missing ';' before '<'
    missing type specifier - int assumed
    unexpected tokens preceding ';'


    I can't figure out what I'm doing wrong here. How do I move the class's functions into a different file?

    For reference, the main program was:

    Code:
    #include <iostream>
    using namespace std;
    
    #include <vector>
    #include "testClass.h"
    
    
    int main()
    {
    	testClass t;
    	return 0;
    }
    Thanks in advance for any help. I've been pounding away at this for hours

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you use a vector in your header file, you need to #include <vector> in your header file, not in your main.cpp.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    #include <vector> in testClass.h instead. The #include serves no purpose in main's file since you do not instantiate a vector in your main function.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    Adding vector in the header file didn't change anything. (And if adding that line had helped, I would wonder why the lack of #include <vector> in the one-file version didn't lead to problems.)

    Edit: aha! adding

    Code:
    #include <iostream>
    using namespace std;
    #include <vector>
    made it work. Thank you!

    Which leaves the question: why did I not need to add all this when all the class-related code was in the same file?
    Last edited by vincible; 01-20-2010 at 09:31 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vincible
    Which leaves the question: why did I not need to add all this when all the class-related code was in the same file?
    It is a compounded mistake. You should have fully qualified the name vector<int> in the header file, i.e., std::vector<int>. In fact, using directives should not be used at namespace scope in header files, and if they are used in source files, they should be used after headers are included.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    Quote Originally Posted by laserlight View Post
    using directives should not be used at namespace scope in header files
    I did not know this, but after googling it makes sense. Thank you.

    I still do not understand why the original code compiled and ran properly, though.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vincible
    I still do not understand why the original code compiled and ran properly, though.
    You probably did something like this in the original code:
    Code:
    #include <vector>
    
    using namespace std;
    
    #include "testClass.h"
    
    // ...
    So, the <vector> inclusion and using directive were both visible to the contents of the header file, after the contents had been included in the source file.

    The moral of the story: make sure that each header file includes the necessary headers that it depends on, or supplies forward declarations otherwise, and do not use using directives and using declarations at namespace scope in header files, or before a header file inclusion (if that using directive/declaration would then affect the contents of the included header).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting template classes between .h and .cpp files
    By Neo1 in forum C++ Programming
    Replies: 12
    Last Post: 01-16-2010, 05:47 AM
  2. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  3. Get .cpp and .h files to communicate...
    By yaya in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2008, 12:45 AM
  4. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 06:37 PM
  5. Class files (.h and .cpp
    By Todd in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 03:07 PM