0

I'm trying to write a c++ program that reads some .txt file, and I've tried several ways of opening the file and verifying it exists but neither compiles. The following are extracts from my code:

#include <fstream>
#include <string>
#include <filesystem>

#include "read_graph.hpp"

ReadGraph::ReadGraph(const std::string filename) : _filename(filename) {}

bool ReadGraph::exists() {
  std::ifstream = gfile;               // case (1)
  std::filesystem::path p(_filename);  // case (2)

}
bool ReadGraph::readable() { return false; }

int main() { return 0; }

The following is my CMakeLists.txt

cmake_minimum_required(VERSION 3.14)

project(graph_generator VERSION 0.1 LANGUAGES CXX)

add_library(read_graph
    read_graph.cpp
)

target_compile_features(read_graph PUBLIC cxx_std_20)
add_executable(exec
    read_graph.cpp
)

When I try to compile case (1) (i.e case (2) is commented), I get the following error: error: expected unqualified-id std::ifstream = gfile;.

When I try to compile case (2), I get error: no member named 'filesystem' in namespace 'std' std::filesystem::path p(_filename);

I don't understand what's going on here, as I included all the correct headers, and these are standard libraries in C++ 17 and 20, so from what I understand I don't need to pass additional flags into my CMakeLists.

Thanks in advance

  • 2
    `std::ifstream = gfile; ` is wrong, because you can't assign something to a class. The error message means, that the compiler expected a variable name after `std::ifstream`. Maybe the following error is also a result of this. – Lukas-T Oct 27 '19 at 09:57
  • For case 1, `std::ifstream = gfile;` is an invalid statement. What is `gfile` supposed to be? Perhaps you meant something more like `std::ifstream gfile(_filename);` instead? For case 2, are you sure you are running your compiler in C++17 mode? – Remy Lebeau Oct 27 '19 at 09:57
  • Did you tell your compiler to use the C++17 features as described here: https://gcc.gnu.org/projects/cxx-status.html#cxx17 ? – Christophe Oct 27 '19 at 10:05
  • Changing to `std::ifstream gfile(_filename);` yields the same error. As for C++17, is `target_compile_features(read_graph PUBLIC cxx_std_20)` in CMakeListst.txt sufficient to tell the compiler I'm in C++17? Because I compile with `make` after running `cmake`. – konovification Oct 27 '19 at 10:12
  • @RemyLebeau My bad, made a typo with an `=` sign, which is why it wasn't calling the constructor as I expected. But case (2) is still not working. – konovification Oct 27 '19 at 10:51
  • Welcome to Stackoverflow! Did you try adding the CMake code in [this question](https://stackoverflow.com/q/45688522/3987854) to your CMake (try substituting `20` for `17` as the standard)? – Kevin Oct 27 '19 at 14:01

0 Answers0