Errors creating std::vector of local structure - c++

#include <vector>
int main() {
struct st { int a; };
std::vector<st> v;
for (std::vector<st>::size_type i = 0; i < v.size(); i++) {
v.operator[](i).a = i + 1; // v[i].a = i+1;
}
}
The above code gives the following errors when compiled with GNU g++ compiler.
test.cpp: In function ‘int main()’:
test.cpp:6:19: error: template argument for ‘template<class _Alloc> class std::allocator’ uses local type ‘main()::st’
test.cpp:6:19: error: trying to instantiate ‘template<class _Alloc> class std::allocator’
test.cpp:6:19: error: template argument 2 is invalid
test.cpp:6:22: error: invalid type in declaration before ‘;’ token
test.cpp:7:24: error: template argument for ‘template<class _Alloc> class std::allocator’ uses local type ‘main()::st’
test.cpp:7:24: error: trying to instantiate ‘template<class _Alloc> class std::allocator’
test.cpp:7:24: error: template argument 2 is invalid
test.cpp:7:37: error: expected initializer before ‘i’
test.cpp:7:44: error: ‘i’ was not declared in this scope
test.cpp:7:50: error: request for member ‘size’ in ‘v’, which is of non-class type ‘int’
test.cpp:8:20: error: request for member ‘operator[]’ in ‘v’, which is of non-class type ‘int’
Why I am not able to create vector of structures?

Before C++11, you couldn't instantiate templates with local classes. You have two options:
1) Put the st definition outside of main
#include <vector>
struct st { int a; };
int main()
{
std::vector<st> v;
}
2) Compile with a c++11 compiler

Related

"Cpr" C++ request library returning internal error

I wrote a get request code in cpr library and I sent a get request to this url but its returning an internal error.
#include <iostream>
#include <cpr/cpr.h>
#include <string>
int main()
{
cpr::Response r = cpr::Get(cpr::Url{"http://api.plos.org/search?q=title:DNA"});
int infosf = r.status_code;
std::cout << infosf;
return 0;
}
headers loaded fine but its returning internal error
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\curl_container.h:9:0,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\payload.h:6, from c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\api.h:16,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\cpr.h:4, from .\meantest.cpp:4:
c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\curlholder.h:22:17: error: 'mutex' in namespace 'std' does not name a type static std::mutex curl_easy_init_mutex_;
^~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\cpr.h:4:0,
from .\meantest.cpp:4:
c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\api.h: In function 'cpr::AsyncResponse cpr::GetAsync(Ts ...)':
c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\api.h:45:32: error: return type 'using AsyncResponse = class std::future<cpr::Response> {aka class std::future<cpr::Response>}' is incomplete
AsyncResponse GetAsync(Ts... ts) {
^
c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\api.h: In function 'cpr::AsyncResponse cpr::PostAsync(Ts ...)':
c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\api.h:69:33: error: return type 'using AsyncResponse = class std::future<cpr::Response> {aka class std::future<cpr::Response>}' is incomplete
AsyncResponse PostAsync(Ts... ts) {
^
c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\api.h: In function 'cpr::AsyncResponse cpr::PutAsync(Ts ...)':
c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\api.h:93:32: error: return type 'using AsyncResponse = class std::future<cpr::Response> {aka class std::future<cpr::Response>}' is incomplete
AsyncResponse PutAsync(Ts... ts) {
^
c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\api.h: In function 'cpr::AsyncResponse cpr::HeadAsync(Ts ...)':
c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\api.h:117:33: error: return type 'using AsyncResponse = class std::future<cpr::Response> {aka class std::future<cpr::Resture<cpr::Response> {aka class std::future<cpr::Response>}' is incomplete
AsyncResponse OptionsAsync(Ts... ts) {
^
c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\api.h: In function 'cpr::AsyncResponse cpr::PatchAsync(Ts ...)':
c:\mingw\lib\gcc\mingw32\6.3.0\include\cpr\api.h:192:34: error: return type 'using AsyncResponse = class std::future<cpr::Response> {aka class std::future<cpr::Response>}' is incomplete
AsyncResponse PatchAsync(Ts... ts) {
I tried turning off firewall still it didnt work

How to handle nested type template in C++

Can anyone help me with this errors. When i compile this simple program
#include<queue>
using namespace std;
template<typename Queue>
int qtest(Queue & queue,typename Queue::Type item)
{
return 0;
}
int main()
{
std::queue<int> q;
int t = qtest(q,3);
}
I get the errors like below
In function 'int main()':
error: no matching function for call to 'qtest(std::queue<int>&, int)'
note: candidate is:
note: template<class Queue> int qtest(Queue&, typename Queue::Type)
note: template argument deduction/substitution failed:
In substitution of 'template<class Queue> int qtest(Queue&, typename Queue::Type) [with
Queue = std::queue<int>]':
required from here
error: no type named 'Type' in 'class std::queue<int>'
warning: unused variable 't' [-Wunused-variable]
std::queue doesn't have a member type called Type. That's what the compiler is telling us. I'm guessing what you're looking for is std::queue<int>::value_type.
template<typename Queue>
int qtest(Queue & queue,typename Queue::value_type item)
{
return 0;
}
Reference: cppreference

C++ program to output a list of pairs of the form L:N where L is a line number and N is the number of occurrences of the given word

#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <string>
using namespace std;
int main(int argc, const char *argv[]) {
ifstream inflie(argv[1]);
int currentLine = 1;
vector<string> words;
map<vector<string>, int> freq;
string line;
while (getline(infile, line)) {
istringstream inputString(line);
currentLine++
string word;
while(inputString >> word){
words.push_back(pair(word, currentLine));
freq[words]++;
}
map<vector<string>, int>::const_iterator iter;
for (iter=freq.begin(); iter != freq.end(); ++iter) {
cout << iter->second << " " << iter->first << endl;
}
return 0;
}
this is what I have so far. Its giving me multiple errors when I try to compile. please keep in mind that last c++ stuff I did was in highschool nearly two years ago. I really have absolutely no idea what I'm doing.
Here are my errors (this is really sad):
tabulate.cc: In function `int main(int, const char**)':
tabulate.cc:11: error: `vector' was not declared in this scope
tabulate.cc:11: error: expected primary-expression before '>' token
tabulate.cc:11: error: `words' was not declared in this scope
tabulate.cc:12: error: `vector' cannot appear in a constant-expression
tabulate.cc:12: error: wrong number of template arguments (1, should be 4)
/usr/local/lib/gcc/sparc-sun-solaris2.9/3.4.6/../../../../include/c++/3.4.6/bits/stl_map.h:92: error: provided for `template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map'
tabulate.cc:12: error: expected unqualified-id before ',' token
tabulate.cc:12: error: expected unqualified-id before "int"
tabulate.cc:14: error: `infile' was not declared in this scope
tabulate.cc:15: error: variable `std::istringstream inputString' has initializer but incomplete type
tabulate.cc:17: error: expected `;' before "string"
tabulate.cc:18: error: `word' was not declared in this scope
tabulate.cc:19: error: missing template arguments before '(' token
tabulate.cc:20: error: `freq' was not declared in this scope
tabulate.cc:20: warning: unused variable 'freq'
tabulate.cc:22: error: `vector' cannot appear in a constant-expression
tabulate.cc:22: error: wrong number of template arguments (1, should be 4)
/usr/local/lib/gcc/sparc-sun-solaris2.9/3.4.6/../../../../include/c++/3.4.6/bits/stl_map.h:92: error: provided for `template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map'
tabulate.cc:22: error: expected unqualified-id before ',' token
tabulate.cc:22: error: expected unqualified-id before "int"
tabulate.cc:23: error: `iter' was not declared in this scope
tabulate.cc:23: error: `freq' was not declared in this scope
tabulate.cc:14: warning: unused variable 'infile'
tabulate.cc:27: error: expected `}' at end of input

C++11 decltype: How to declare the type that a pointer points to?

I have the following code:
#include <memory>
int main()
{
int* a = new int(2);
std::unique_ptr<decltype(*a)> p(a);
}
which leads to these error message:
In file included from a.cpp:1:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/memory:81:
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/unique_ptr.h:138:14: error: '__test' declared as a pointer to a reference of type 'int &'
static _Tp* __test(...);
^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/unique_ptr.h:146:35: note: in instantiation of member class 'std::unique_ptr<int &,
std::default_delete<int &> >::_Pointer' requested here
typedef std::tuple<typename _Pointer::type, _Dp> __tuple_type;
^
a.cpp:7:35: note: in instantiation of template class 'std::unique_ptr<int &, std::default_delete<int &> >' requested here
std::unique_ptr<decltype(*a)> p(a);
^
In file included from a.cpp:1:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/memory:81:
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/unique_ptr.h:227:33: error: 'type name' declared as a pointer to a reference of type 'int &'
is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>>
^
a.cpp:7:35: note: in instantiation of template class 'std::unique_ptr<int &, std::default_delete<int &> >' requested here
std::unique_ptr<decltype(*a)> p(a);
^
2 errors generated.
I understand the reason is that the unique_ptr template expects type int, but decltype(*a) gives int&. In the case that int is a very long and complicated type, how can I make this code work with decltype?
Use std::decay_t. This is the conversion that is applied when you pass an argument to a function by value.
You can use a typedef inside a templated class and then use template specialisation, like this
template<typename T> struct unref {
typedef T raw;
};
template<typename T> struct unref<T&> {
typedef T raw;
};
int main() {
int* a = new int(2);
std::unique_ptr<unref<decltype(*a)>::raw> p(a);
}

pretty print of boost::multi_array example fails

I have this smart and cool example:
#include <iostream>
#include "boost/multi_array.hpp"
#include "boost/array.hpp"
#include "boost/cstdlib.hpp"
template <typename Array>
void print(std::ostream& os, const Array& A)
{
typename Array::const_iterator i;
os << "[";
for (i = A.begin(); i != A.end(); ++i) {
print(os, *i);
if (boost::next(i) != A.end())
os << ',';
}
os << "]";
}
void print(std::ostream& os, const double& x)
{
os << x;
}
int main()
{
typedef boost::multi_array<double, 2> array;
double values[] = {
0, 1, 2,
3, 4, 5
};
const int values_size=6;
array A(boost::extents[2][3]);
A.assign(values,values+values_size);
print(std::cout, A);
return boost::exit_success;
}
But if I try to compile it: g++ -I/usr/include/boost/ b.cpp I get this error:
b.cpp: In function ‘void print(std::ostream&, const Array&) [with Array = double]’:
b.cpp:12: instantiated from ‘void print(std::ostream&, const Array&) [with Array = boost::detail::multi_array::const_sub_array<double, 1u, const double*>]’
b.cpp:12: instantiated from ‘void print(std::ostream&, const Array&) [with Array = main()::array]’
b.cpp:32: instantiated from here
b.cpp:9: error: ‘double’ is not a class, struct, or union type
b.cpp:11: error: request for member ‘begin’ in ‘A’, which is of non-class type ‘const double’
b.cpp:9: error: ‘double’ is not a class, struct, or union type
b.cpp:9: error: request for member ‘end’ in ‘A’, which is of non-class type ‘const double’
b.cpp:9: error: ‘double’ is not a class, struct, or union type
b.cpp:9: error: ‘double’ is not a class, struct, or union type
b.cpp:9: error: ‘double’ is not a class, struct, or union type
b.cpp:13: error: request for member ‘end’ in ‘A’, which is of non-class type ‘const double’
b.cpp:9: error: ‘double’ is not a class, struct, or union type
shell returned 1
What was wrong? It seems to not understand the difference between the first and the second print function. Maybe some compiler options I missed?
EDIT:
If i use template<> as Craig H answer i resolve the problem in a isolated example.cpp. But if i put the 2 functions in a separate .h file in my project, the error appears again!
In this example you created a template function as your first print function then declare another function that implements a specific version of that template without template specializing. I think if you change the following line
void print(std::ostream& os, const double& x)
to
template<> void print<double>(std::ostream& os, const double& x)
your problem should go away.