get directory from file path c++

The initialisation is incorrect as you need to escape the backslashes: string filename = “C:\\MyDirectory\\MyFile.bat”; To extract the directory if present: string directory; const size_t last_slash_idx = filename.rfind(‘\\’); if (std::string::npos != last_slash_idx) { directory = filename.substr(0, last_slash_idx); }

C++ won’t let me make friends

When you compile Mesh.cpp, it includes Mesh.h, which includes MeshList.h, which starts to include Mesh.h but stops early because _MESH_H is now defined. Then (back in MeshList.h) there’s a reference to Mesh — but that hasn’t been declared yet. Hence, e.g., your C2143 error.

Why can a braced-init-list not be used as an expression in a conditional operator?

Well, here’s what a note in the C++11 standard, [dcl.init.list] p1 says: List-initialization can be used as the initializer in a variable definition ([dcl.init]) as the initializer in a new expression ([expr.new]) in a return statement ([stmt.return]) as a function argument ([expr.call]) as a subscript ([expr.sub]) as an argument to a constructor invocation ([dcl.init], [expr.type.conv]) … Read more

__attribute__((format(printf, 1, 2))) for MSVC?

Using SAL Annotations you can use _Printf_format_string_ (as of VS2k8 or VS2k10) or __format_string (for VS2k5): #undef FORMAT_STRING #if _MSC_VER >= 1400 # include <sal.h> # if _MSC_VER > 1400 # define FORMAT_STRING(p) _Printf_format_string_ p # else # define FORMAT_STRING(p) __format_string p # endif /* FORMAT_STRING */ #else # define FORMAT_STRING(p) p #endif /* _MSC_VER … Read more

C++20 constexpr vector and string not working

Your program is actually ill-formed, though the error may be hard to understand. constexpr allocation support in C++20 is limited – you can only have transient allocation. That is, the allocation has to be completely deallocated by the end of constant evaluation. So you cannot write this: int main() { constexpr std::vector<int> v = {1, … Read more

How does switch compile in Visual C++ and how optimized and fast is it?

A switch is often compiled to a jump-table (one comparison to find out which code to run), or if that is not possible, the compiler may still reorder the comparisons, so as to perform a binary search among the values (log N comparisons). An if-else chain is a linear search (although, I suppose, if all … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)