I am using the OpenCV library for my project and using namespace "cv" and "std" for my source files.I want to use the string split function to split a string based on a delimiter .But the "split" function is defaulting to the namespace "cv" and is showing errors related to wrong arguments as it is expecting arguments related to the OpenCV slpit function.
What can be done to overcome this problem?I see that the same function worked fine in another source file even though that one has got both std and cv anmespace.
These are the headers I am including in both files -
#include "stdafx.h"
#include <unordered_map>
#include <iostream> // for standard I/O
#include <fstream>
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
Avoid putting using namespace std; and using namespace cv; in your code, especially in headers.
If you can't stop yourself, fully qualify the function where you need to, e.g.
std::split(...)
As long as the argument sets for the cv and std functions are disjoint, you shouldn't introduce any ambiguity in the overload resolution (both would be injected into the global namespace).
Based on a comment showing split is from a 2013 proposal much more likely is that your compiler just doesn't implement std::split yet.
All you need to do is prefix the function with its' namespace
For instance
std::split(...);
You can overcome this by using explicit namespace specifiers.
std::split
Related
#include <iostream>
using namespace std;
int main(){
swap(a[i],a[j])
}
In this the swap function work automatically or I need to create a another swap function to solve this?
swap is not a keyword in C++ but rather a function from the C++ standard library. So to use it you need to bring in the function from the appropriate C++ standard library header. Unfortunately std::swap has been rather itinerant since it first made it into the C++ standard:
Up to but not including C++11, you need to #include <algorithm>
From C++11 you need to #include <utility>
From C++17 you can #include <string_view> instead
Relying on the implicit inclusion of the header means you're not writing portable C++.
I'm a newbie to C++, and programming for the most part, just started learning a few days ago, and I'm a bit confused about this.
Is the string variable type in the standard namespace?
I found I can use strings without using #include <string>. I can also use using namespace std;, to activate the use of strings, or std::string mystring; for example.
I know that using using namespace std; allows for the use of all commands/functions within the standard namespace, ie cout.
If string is within the standard namespace, is #include <string> the same as saying using std::string; ?
std::string is a class defined in the standard library header <string>. Like all names of the standard library, it is declared in the namespace std.
is #include <string> the same as saying using std::string; ?
No. Those have two entirely different meanings.
#include <string> means "include the content of the header <string> into this file". The header contains the definition of the class std::string among other things.
using std::string; essentially means "declare the identifier string as a type alias to std::string in the current namespace".
"I found I can use strings without using #include <string>"
That's coincidence. You included another header that by chance included <string>. This is allowed in C++, and pretty common for e.g. <sstream>. But you can't rely on this. If you need std::string, you should #include <string>.
This will include std::string, from the std namespace. This namespace is actually defined by multiple headers. Unlike classes, namespaces do not need to be defined in one header; they can be spread over multiple.
I am trying to move a few tests into the XCTest target of my application in Xcode. The library I am testing functionalities for is large, and I include its header files inside my tests.mm file (my Obj-C++ testing file where all unit test cases are).
For one specific "Size" type, Compiler reports a series of ambiguous clashes with the Size type already defined as part of the SDK for MacOS10.13 (in /usr/include). Here is how the #include set for my testing file looks like:
#import <XCTest/XCTest.h>
#include <ql/quantlib.hpp>. <---- defines Size, which clashes with MacTypes.h definition
#include <Analytics/all.hpp>
#include <boost/timer.hpp>
#include <utility>
#include <typeinfo>
#include <thread>
#include <atomic>
#include <mutex>
#include <random>
#include <condition_variable>
#include <future>
using namespace src::Utilities;
using namespace src::Simulation;
using namespace src::MarketData;
using namespace src::detail;
using namespace std::placeholders;
//...More code down here, but not problematic
is the file which contains the definition for Size. Can someone please help spot where I may have gone wrong, especially that when I run the tests as part of a giant Main() function, it all works fine.
Thanks,
Amine
Update: After some research, I think I found what was going on with this issue: "using namespace xxxx" inside Objective-C++ testing framework instead of fully qualifying the data types with xxxx:: clashes with native Apple API types (Size, Handle, ptr, etc...). Full namespace qualification of C++ data types is required in this case to avoid ambiguity.
I would like to use the string class.
Should I also involve using namespace std;?
I thought #include <string> would be enough but in CLion when only one of these two (namespace or include) is absent, there are some errors.
What makes things more complicated, is that there is <string> or <strings.h>. What's the difference?
<string> is C++ and provides the std::string class.
<string.h> is C (the C++ equivalent is <cstring>) and only provides functions to work on char*.
Don't use using namespace std; (see the C++ Core Guidelines).
In C++ the using statement is not related to including the functionality of that namespace (or type). Instead, it allows you to use the namespace in the statement without a namespace prefix in the rest of the current scope (or compilation unit if you have the statement in global scope) after the using statement.
So you could either write
#include <string>
std::string my_string;
or
#include <string>
using namespace std;
string my_string;
As others have mentioned - the first version is more recommended than the second, because there's usually a reason that things are in their own namespace (such as the std namespace here). If you have a blanket using statement you may get unexpected name clashes or other issues. This is especially true if you do it in a header file, which you should never do unless you know exactly what the results will be and where.
In my header file I'm getting the
error: ‘string’ has not been declared
error but at the top of the file I have #include <string>, so how can I be getting this error?
string resides in the std namespace, you have to use std::string or introduce it into the scope via using directives or using declarations.
Use
std::string var;
or
using namespace std;
string var;
String is in a std namespace so you must let your compiler know.
Most of the classes and class templates, and instances of those classes, defined by the Standard Library (like string and cout) are declared in the std:: namespace, not in the global namespace. So, whenever you want to use (say) string, the class is actually std::string.
You can add that namespace prefix explicitly every time you use such a class, but that can become tedious. In order to help, you can add using declarations for those classes you use frequently.
So, in your case, you could add lines like the following, generally somewhere shortly after the corresponding #include lines:
#include <string>
#include <iostream>
using std::string;
using std::cout; // Added as illustrative examples of other
using std::endl; // ... elements of the Standard Library
Or, if you have a compiler that supports the C++17 Standard (or later), you can put more than one class in the same statement:
using std::string, std::cout, std::endl; // C++17 or later
But, beware of using the generic, "cover-all" using namespace std; directive (even though that is actually valid C++ syntax). See: Why is "using namespace std;" considered bad practice?
For a more general introduction to, and overview of, the Standard Template Library (now, more correctly known as the C++ Standard Library), see the tag-Wiki for the stl tag.