Trying to sort an array of Integers and after some googling, came across the solution using std::sort accompanied by this error: namespace "std" has no member "sort".
Just to disqalify any qualms that I'm not using the std namespace, here is my header:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
Add:
#include <algorithm>
as stated in the std::sort() reference page.
See Using std Namespace, Why is "using namespace std" considered bad practice? and many other questions on SO discussing using namespace std;.
Related
I am having an error while trying to convert a string vector to a double vector. The error keeps saying:
error: 'stod' was not declared in this scope
Even though I enabled C++11 for my compiler and I used #include <string> I also used using namespace std; And it still didn't work.
The code is is down below:
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;
using stod;
transform(userNums.begin(), userNums.end(), back_inserter(convUserNums), [](const string & astr){ return stod( astr) ; } ) ;
stod is defined in the namespace std. You should call it like std::stod.
Or put using namespace std; above.
Be sure to include the necessary header and using directive.
#include <string>
using std::stod;
int main( const int, const char** )
{
stod( "3.4" );
return EXIT_SUCCESS;
}
Please find the std::stod documentation here. Note that this function will throw both std::invalid_argument and std::out_of_range exceptions. A try-catch block is highly recommended.
The example was built with g++ -o main main.cpp -std=c++11 under gcc version 4.9.2 (Debian 4.9.2-10).
I am trying to compile C++ code shown below but I got an error saying,
In file included from src/LM.h:3:0,
from src/LM.cpp:1:
src/common.h:30:13: error: ‘hash’ is already declared in this scope
using tr1::hash;
This is the command I used to compile the files below.
g++ -std=c++11 -Wall src/Foo.cpp
Foo.cpp
#include "Foo.h"
...
Foo.h
#ifndef FOO_H
#define FOO_H
#include "common.h"
//more code here
#endif
common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#include <iostream>
#include <fstream>
#include <cmath>
#include <cassert>
#include <cstdlib>
#include <utility>
#include <vector>
#include <string>
#include <array>
#include <algorithm>
#include <set>
#include <tr1/unordered_map>
#include <tr1/functional>
namespace std {
using tr1::unordered_map;
using tr1::hash;
} // namespace std
using namespace std;
//more code here
#endif
I want the source code to use std::tr1::unordered_map and std::tr1::hash rather than std::unordered_map and std::hash(Actually I am making some modifications to distributed files which does uses std::tr1::unordered_map and std::tr1::hash).
What is possibly wrong with my codes?
UPD:
https://github.com/clab/fast_align/blob/master/src/port.h seems to do the same thing as mine. However, this compiles without any problem... Have any idea?
There is already std::hash in C++11. You cannot redefine it. You can use another name for tr1::hash.
Probably the best idea (if you really want to use std::tr1::hash/std::tr1::unordered_map instead of C++11 structures) is to write your own namespace in which using all structures, that you want without std::hash/std::unordered_map.
namespace common
{
using std::tr1::hash;
using std::tr1::unordered_map;
using std::vector;
// and so on
}
In the boost libraries, there are often examples of including the library like:
#pragma once
#include <boost/property_tree/ptree.hpp>
using boost::property_tree::ptree;
Throughout my program I have been importing namespaces like this:
#include "../MyClass.h"
using namespace MyClassNamespace;
Can someone please explain:
The difference between using and using namespace;
What the advantage of negating the use of using namespace in favour of using;
The differences in forward declaring using and using namespace;
Thanks
using namespace makes visible all the names of the namespace, instead stating using on a specific object of the namespace makes only that object visible.
#include <iostream>
void print(){
using std::cout;
using std::endl;
cout<<"test1"<<endl;
}
int main(){
using namespace std;
cout<<"hello"<<endl;
print();
return 0;
}
while using "using namespace std" all the elements under the scope of std are made available under scope of the function.
while using "using std::cout" we explicitly mention what element under the std is required for the function ,without importing all the elements under std.
this is my first answer in stack overflow please correct me if iam wrong!!
I tried to follow the instructions on
http://msdn.microsoft.com/en-us//library/vstudio/dd728073.aspx
I am using VS2010.
I added the following lines to my CPP file:
#include <windows.h>
#include <ppl.h>
#include <iostream>
#include <random>
using namespace concurrency;
But then the compiler tells me "Error C2871: 'concurrency': No namespace with that name exists.'"
Does anybody see what I did wrong?
Thank you!
Great, in VS2010 it has to be
using namespace Concurrency;
instead.
Nice day to all!
I try to compile a little example from boost docs pdf's:
#include "boost/icl/interval.hpp"
#include "boost/icl/interval_set.hpp"
#include "boost/date_time/posix_time/posix_time_duration.hpp"
#include <boost/icl/ptime.hpp>
#include <boost/date_time.hpp>
#include <iostream>
#include <string>
using namespace std;
using namespace boost;
using namespace boost::icl;
using namespace boost::posix_time;
using namespace boost::gregorian;
...
interval<boost::posix_time::seconds>::type news(seconds(), make_seconds("20:15:00"));
interval<seconds>::type talk_show(make_seconds("22:45:30"), make_seconds("23:30:50"));
interval_set<seconds> myTvProgram;
myTvProgram.add(news).add(talk_show);
// Iterating over elements (seconds) would be silly ...
for(interval_set<seconds>::iterator telecast = myTvProgram.begin();
telecast != myTvProgram.end(); ++telecast)
//...so this iterates over
return 0;
I've error:
error C3861: 'make_seconds': identifier not found
How to make correct convert? Thanks.