'stoi' and 'to_string' is not a member of 'std' [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have some code, it's quite large so I'll just create a snapshot of it here:
int l = 3;
vector<int> weights;
void changeWeights(int out){
for (int i = 0; i < weights.size(); i++){
int w = std::stoi(std::to_string(weights[i])) -
out*std::stoi(std::to_string(weights[i]));
if (w < -l){
w = -l;
} else if(w > l){
w = l;
}
weights.assign(i, w);
}
}
I get errors on both the 'stoi' and 'to_string' function calls in the form of
Main.cpp:35:21: error: ‘stoi’ is not a member of ‘std’
int w = std::stoi(std::to_string(weights[i])) -
^
Main.cpp:35:31: error: ‘to_string’ is not a member of ‘std’
int w = std::stoi(std::to_string(weights[i])) -
^
Main.cpp:36:17: error: ‘stoi’ is not a member of ‘std’
out*std::stoi(std::to_string(weights[i]));
^
Main.cpp:36:27: error: ‘to_string’ is not a member of ‘std’
out*std::stoi(std::to_string(weights[i]));
I have read some similar queries whereby the answer was to add in -std=c++11 or -std=c++0x when compiling - both these solutions did not work. Another solution suggested a bug in the compiler version but it's not the compiler I am using I do not think. I am using g++ (GCC) 5.0.0 20141005 (experimental) version on a 64x Apple Macbook Pro.

Usage of stoi() and to_string() in this part of code is pretty weird, and completely unnecessary. You can simply write
int w = weights[i] - out * weights[i];
To use std::stoi() and std::to_string() you need to have a proper
#include <string>
statement, and the c++11 language options set (see the links to the documentation reference above).

Related

Getting a compilation error while using (<=>) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 days ago.
Improve this question
New to C++ here, following outdated training videos...
Trying to compile this simple code, but getting the below error:
#include <iostream>
int main(){
auto result = (10 <=> 20) > 0;
std::cout << result << std::endl;
}
Error I get is:
prog.cc: In function 'int main()':
prog.cc:4:25: error: expected primary-expression before '>' token
4 | auto result = (10 <=> 20) > 0;
|
What am I doing wrong?

a non-type template parameter cannot have type [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
For anyone stumbling upon this question in the future, the original question was: "How to get the simplest example (Point) from this article to compile with GCC or CLANG?"
Edit 1 shows the smallest possible code that fails with CLANG but compiles with GCC (both with -std=c++2a).
Edit 2 shows further code added, that breaks GCC as well.
The author of the article (#BarryRevzin) was kind enough to comment the reason why this won't work yet, thank you Barry!
Edit 1:
Simplified code below works with gcc 9.3.0 but not with clang 10.0.0:
struct Point {
int x = 0;
int y = 0;
};
template <Point> // ok in C++20
void takes_tmpl_point();
int main()
{
// EMPTY
}
Edit 2:
The original code, as per the author, won't work on GCC or CLANG as of yet due to the compilers being behind the standard a bit. Original code below:
struct Point {
int x = 0;
int y = 0;
};
template <Point> // ok in C++20
void takes_tmpl_point();
int main()
{
takes_tmpl_point<{.x=1, .y=2}>(); // x=1, y=2
}
This will result in the following compilation error on GCC 9.3:
test.cpp: In function ‘int main()’:
test.cpp:11:35: error: no matching function for call to ‘takes_tmpl_point<{1, 2}>()’
11 | takes_tmpl_point<{.x=1, .y=2}>(); // x=1, y=2
| ^
test.cpp:7:6: note: candidate: ‘template<Point <anonymous> > void takes_tmpl_point()’
7 | void takes_tmpl_point();
| ^~~~~~~~~~~~~~~~
test.cpp:7:6: note: template argument deduction/substitution failed:
test.cpp:11:35: error: could not convert ‘{1, 2}’ from ‘<brace-enclosed initializer list>’ to ‘Point’
11 | takes_tmpl_point<{.x=1, .y=2}>(); // x=1, y=2
| ^
And the following error on clang 10.0.0:
test.cpp:6:16: error: a non-type template parameter cannot have type 'Point'
template <Point> // ok in C++20
^
test.cpp:11:21: error: expected expression
takes_tmpl_point<{.x=1, .y=2}>(); // x=1, y=2
^
2 errors generated.
Compilers used:
clang: clang version 10.0.0-4ubuntu1
gcc: gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
Clang just doesn't implement class types as non-type template parameters yet, see P1907 in this table.
gcc does implement them but there's actually an issue here. The grammar for template-argument doesn't actually allow for a braced-init-list. This is a clear language defect (there was never a reason to have such a thing before P1907 but now there's certainly no reason to not have it). This is a language bug at the moment. Nevertheless, gcc went ahead and does support a braced-init-list as a template argument... just not a designated-initializer-list.
So that blog post of mine is running ahead of the actual language by a bit... Until the language catches up, even though this is technically unsupported:
takes_tmpl_point<{.x=1, .y=2}>();
This is definitely valid:
takes_tmpl_point<Point{.x=1, .y=2}>();

Ambiguating new declaration of 'void visualizar()' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am new in C++ and i have a little error in this program "Hello world"
//ejemplo funciones definidas por el usuario
#include<iostream>
using namespace std;
int visualizar();
int main()
{
visualizar();
return 0;
}
void visualizar() //Here is the error
{
cout<<"Hola mundo guay\n";
}
Error:
C:\Users\lisan\OneDrive\Desktop\c++\EjemploFunciones.cpp In function 'void visualizar()':
15 17 C:\Users\lisan\OneDrive\Desktop\c++\EjemploFunciones.cpp [Error] ambiguating new declaration of 'void visualizar()'
6 5 C:\Users\lisan\OneDrive\Desktop\c++\EjemploFunciones.cpp [Note] old declaration 'int visualizar()'
What does this error mean? What caused it? How do I fix it?
In your prototype of visualizar, you declared it as
int visualizar();
However, when you defined it, you wrote
void visualizar() { ... }
Notice that the return types are different. Did you mean to use void throughout?

unexpected compilation error in a function call (C++) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a template function makeMatrix(), code is:
template<size_t N>
void makeMatrix(string dataFilePath, int adjcMatrix[N][N])
{
fstreamExtension fe("adj.txt", ios::in|ios::binary);
string s;
vector<int> temp;
int i = 0;
while(!fe.eof())
{
getline(fe, s);
temp = tokenizeToInt(s, ",\n")); //error: expected ';' before ')' token|
for(int j = 0; j < N; j++)
adjcMatrix[i][j] = temp[j];
i += 1;
}
}
fstreamExtension is a class I created and is included in program through header
#include "fstreamExtension.h", other included headers are iostream string and boost/tokenizer.hpp.
code for tokenizeToInt():
vector<int> tokenizeToInt(string& intString, const char* seperators)
{
vector<int> intValues;
boost::char_separator<char> delims(seperators);
boost::tokenizer<boost::char_separator<char>> tokens(intString, delims);
for (const auto& t : tokens) {
intValues.push_back(atoi(t.c_str()));
}
return intValues;
}
Why it is causing a compilation error in the makeMatrix(), the syntax seems correct, I didn't called it in main(), was compiling some other code then this error popped up when I started a build.
IDE : codeblocks 16.01, gcc.
You should listen to what the compiler tells you. Often the error is simpler than you think:
temp = tokenizeToInt(s, ",\n")); //error: expected ';' before ')' token|
An extra right-parenthesis. The compiler error means "I thought you were done with this command, why are you trying to close another parenthesis-pair?"

C++ compiling errors [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
First of all here is my C++ source:
#include <iostream>
using namespace std;
void number(int x){
cout << "Number is: " << x << endl;
}
int main(){
cin >> int x;
number(x);
return(0);
}
Upon compiling I get the following errors:
file.cpp: In function 'int main()':
file.cpp:9:9: error: expected primary-expressing before 'int'
file.cpp:9:9: error: expected ';' before 'int'
file.cpp:10:9: error: 'x' was not declared in this scope
I compile and run this successfully in CodeBlocks but under Ubuntu with gcc or g++ things fail.
Change
int main(){
cin >> int x;
to
int main(){
int x;
cin >> x;