0

I'm trying to compile a .cpp file using g++ in Linux Ubuntu 10.10 and when i try to compile this code

#include <iostream>                                                             
#include <vector>                                                               
#include <"writeVector.h"                                                       
#include <"insertionSort.h">                                                    
using namespace std;  



int main()                                                                      
{                                                                               
  int n;                                                                        
  int i;                                                                        
  vector<int> V;                                                                
  cout << "Enter the amount of numbers you want to evaluate: ";                 
  cin >> n;                                                                     
  cout << "Enter your numbers to be evaluated: " << endl;                       
  while (V.size() < n && cin >> i){                                             
   V.push_back(i);                                                              
  }   

  InsertionSort(V);                                                             
  write_vector(V);                                                              
  return 0;                                                                     
}   

I have both .h files in the same folder but it keeps saying that my writeVector.h file or folder does not exist.

This is what my writeVector.h file looks like

#include <iostream>                                                             
#include <vector>                                                               
using namespace std;                                                            

template <typename T>                                                          

void write_vector(const vector<T>& V)                                          

{                                                  

  cout << "The numbers in the vector are: " << endl;                            
  for(int i=0; i < V.size(); i++)                                                                                                                             
    cout << V[i] << " ";                                                       

}       

insertionSort.h file

#include <iostream>                                                             
#include <vector>                                                               
using namespace std;                                                            

void InsertionSort(vector<int> &num)                                            
{                                                                               
     int i, j, key, numLength = num.length( );                                  
     for(j = 1; j < numLength; j++)    // Start with 1 (not 0)                  
    {                                                                           
           key = num[j];                                                        
           for(i = j - 1; (i >= 0) && (num[i] < key); i--)   // Smaller values move up                                                                         
          {                                                                     
                 num[i+1] = num[i];                                             
          }                                                                     
         num[i+1] = key;    //Put key into its proper location                  
     }                                                                          
     return;                                                                    
}
Mysticial
  • 464,885
  • 45
  • 335
  • 332
Sean
  • 957
  • 2
  • 11
  • 15
  • your missing a > in the first snippet is that a typo? – T I Jan 01 '12 at 00:19
  • It would be best to include the specific error message you're getting from the compiler. – sarnold Jan 01 '12 at 00:23
  • 1
    Don't put `using namespace std;` in headers, it's bad practice. – Mat Jan 01 '12 at 00:32
  • @mat i'm a noob, why is it bad practice? – Sean Jan 01 '12 at 00:46
  • 2
    @Sean: because it pulls all the names in the `std` namespace into all the code that uses your headers, and that's a _lot_ of names. You should actually consider not `using namespace std;` at all, typing `std::` isn't that much trouble. – Mat Jan 01 '12 at 00:49

2 Answers2

7

Change

#include <"writeVector.h"                                                       
#include <"insertionSort.h">  

to

#include "writeVector.h"                                                       
#include "insertionSort.h"

#include "filename" is used for local header files, which are made by you.

#include <filename> is used for header files Globally included in C++, System header files

there is no syntax like <"filename">

Pheonix
  • 6,049
  • 6
  • 30
  • 48
  • now im getting this error: /tmp/ccF0rvjJ.o: In function `main': sandbox.cpp:(.text+0xd1): undefined reference to `InsertionSort(std::vector >&)' collect2: ld returned 1 exit status ????????????? – Sean Jan 01 '12 at 00:44
  • @Sean That means it compiled successfully. You can't link it alone, since your project contains two C++ source files. – David Schwartz Jan 01 '12 at 01:12
  • Why can't you link the file alone? Because it doesn't contain the code for `InsertionSort`. How can you get it to work? Either by compiling each file alone and then linking them together or by compiling them both together. – David Schwartz Jan 01 '12 at 06:57
  • 1
    For the record, having fixed the call from `num.length()` to `num.size()` and compiled using `g++ sandbox.cpp -o sandbox`, I get no linker errors and `./sandbox` runs as expected. – johnsyweb Jan 01 '12 at 07:38
3

#include <"writeVector.h"

That code is not valid. Either of the below lines would work:

#include "wrtieVector.h"
#include <writeVector.h>

but the latter is reserved for system headers.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • i did what u said now im getting this error: /tmp/ccF0rvjJ.o: In function `main': sandbox.cpp:(.text+0xd1): undefined reference to `InsertionSort(std::vector >&)' collect2: ld returned 1 exit status ???? – Sean Jan 01 '12 at 00:43
  • Show us your `Makefile` or your `g++` compilation command. We can't help otherwise. – Basile Starynkevitch Jan 01 '12 at 07:34