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 am making a simple program, please take a look
#include<iostream>
using namespace std;
int main(int argc,char* argv[])
{
int op=0;
int v[20]=[1, 0];
float Ma=0;
if (argv[1]==1)
{
float S=0;
for(int i=0;i<=20;i++)
{
S=S+v[i];
}
Ma=(double)(S/20);
}
cout<<"Media aritmetica pentru elementele din vector este "<<Ma<<endl;
return 0;
}
I get this error Program.cpp:10:13: error: expected identifier before numeric constant
int v[20]=[1, 0];
^
I am using the gcc from ubuntu for compiling and I'm not really sure if there's anything to it that may cause this. I am a bit new into this.
You probably meant to define an array of 20 int's and initialize its first 2 elements to 1 and 0 respectively.
Well, instead of writing:
int v[20] = [1, 0];
you should have written:
int v[20] = {1, 0};
which means what you want it to mean. Note, however, that the term "vector" has a different meaning typically in C++ - the name of the std::vector container class in the standard library.
Use curly braces instead.
int v[20]={1,0};
Related
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'm just making a program and this happens
I don't understand where is converts like that.
This program is supposed to see how many of each character is in a input string. It is work in progress but I already made a for loop to process the input.
Error:
11:30: error: conversion from 'int' to non-scalar type 'std::vector<int>' requested
Code:
// Example program
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string input;
vector<int> letters = (26,0);
vector<char> alpha = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
cin >> input;
for(int i = 0; i < input.size();i++){
for(int j = 0; j < alpha.size(); j++){
if(input[i]==alpha[j]){
letters[j] ++;
}
}
}
}
Your mistake is that you try to call constructor with 2 parameters, using assign without explicitly showing what are that 2 ints mean. You can choose 2 ways of this implementation:
vector<int> letters = vector<int>(26, 0);//here you call copy constructor
vector<int> letters(26,0); // here you call constructor with 2 parameters
Compiler just don't know what do you mean writing "vector letters = (26,0);" it's could be everything - from just 1 int number to implicit convertion to your classes; So you should to show it explicitly.
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 3 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int* i = new int(75);
double* d = new double(3.14159);
printf("%d\n",*i);
printf("%d\n",*d);
}
In the above code i returns a value of 75 however, d returns 1.
I tried explicitly initializing it as
*d = 3.14159
But the value is still returned as 1.
Can anyone explain what I am doing wrong here?
Use this for printing.
cout<<*i;
cout<<*d
"%f" is the (or at least one) correct format for a double if you want to use printf for printing the value of the double in 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 3 years ago.
Improve this question
I am learning C++ on the fly and am having problems with vectors so I am writing some programs that use vectors to familiarize myself with them.
I was following the advice from this post regarding printing out the value of a vector's size() call:
How can I get the size of an std::vector as an int?
My code is a simple C++ code:
#include <vector>
int main(int argc, char ** argv) {
/* create an int vector of size 10, initialized to 0 */
std::vector<int> int_list[10];
int int_list_size;
int_list_size = static_cast<int>(int_list.size()); // <-- compilation error here
} // End main()
I'm on Ubuntu 16.04 and I get this error:
"error: request for member 'size' in 'int_list', which is of non-class type 'std::vector<int> [10]'
Since the size of the vector int_list is 10, shouldn't size() return 10, which I can then cast to an int?
You are not creating a vector, you are creating an array of vector:
std::vector<int> int_list[10];
You should use:
std::vector<int> int_list(10);
See:
https://www.cplusplus.com/reference/vector/vector/vector/
https://en.cppreference.com/w/cpp/container/vector/vector
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 3 years ago.
Improve this question
Even on passing parameters to the function log, error showing too few arguments to function.
#include <iostream>
using namespace std;
int log(int n, int x){
return (n>1) ? 1 + log(n/x) : 0;
}
int main() {
int n,x;
cin>> n>> x;
cout<< log(n,x);
}
I expect the output of log10(1000) to be 3, but few arguments error is shown.
You forgot the second argument to your log function in the recursive step.
return (n>1) ? 1 + log(n/x, x) : 0;
By the way, you should name your variables something descriptive. For instance instead of using n perhaps use base.
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 5 years ago.
Improve this question
I read on another stackoverflow post (variable length array error when passing array using template deduction) that the following should be possible:
#include <iostream>
int input() {
int a;
std::cin>>a;
return a;
}
int main()
{
const int b = input();
int sum[b];
std::begin(sum);
}
Except that it doesn't seem to work, I still get an similar error.
In function 'int main()':
16:17: error: no matching function for call to 'begin(int [b])'
16:17: note: candidates are:
Followed by information on possible templates it could fit.
You can use std::begin(sum) only when sum is regular array, not when it is a variable length array.
The following is OK.
const int b = 10;
int sum[b];
std::begin(sum);
In your case, b is not known at compile time. For arrays whose length are not known at compile time, it's better to use std::vector instead of relying on a compiler specific extension. The following is OK.
const int b = input(); // You can use int b, i.e. without the const, also.
std::vector<int> sum(b);
std::begin(sum);