-2

I find it difficult to get the exact meaning of these terms in C++. It seems like there is a lot of overlap between one another (at least typedef and namespace). Can you please enlighten me why were these concepts invented in C++; and in what scenarios should we use each one of these ?

Also this discussion is particularly confusing. It says 'typedef' and 'using' are the same. It makes me wonder, why do we have two different terms if they are almost the same ?

Due to poor understanding of these terms I coded the following and got the error shown below:

Files.hpp

#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <algorithm>
#include <fstream>
#include <ostream>
#include <iomanip>
#include <cmath>

class Files {

public:

//@Brief: We create some short forms for long type names
typedef boost::filesystem FS; //! Short form for boost filesystem
// Short form for file name pairs (for example, <200.jpg, 200>)
typedef std::pair<FS::path, int> file_entry;
// Short form for vector of tuples
typedef std::vector<file_entry> vec;
// Short form for iterator of type, boost::filesystem::directory_iterator
typedef FS::directory_iterator dirIter;
};

The following is the make error that I receive:

...../include/Files.hpp:10:20: error: ‘filesystem’ in namespace ‘boost’ does not name a type
 typedef boost::filesystem FS; //! Short form for boost filesystem
Arun Kumar
  • 490
  • 5
  • 20

2 Answers2

2

boost::filesystem is a namespace, not a type. So you can do this:

namespace FS = boost::filesystem;
John Zwinck
  • 207,363
  • 31
  • 261
  • 371
  • Pardon me if I am wrong. But this (https://stackoverflow.com/questions/10747810/what-is-the-difference-between-typedef-and-using-in-c11) is the discussion I referred to. It says that typedefs and namespace are the same. Now I am confused. – Arun Kumar Sep 29 '18 at 06:28
  • They are not equivalent. You can use `using` in place of a `typedef` but not the other way around. – Swordfish Sep 29 '18 at 06:38
  • 2
    Typedefs and namespaces are certainly not the same. That question you linked has nothing to do with namespaces (it doesn't even contain the word "namespace"). – John Zwinck Sep 29 '18 at 06:39
2

Since boost::filesystem isn't a type but a namespace. Use a namespace alias at file scope:

namespace FS = boost::filesystem;
Swordfish
  • 1
  • 3
  • 17
  • 42