Invalid conversion from 'int*' to 'int' - c++

This doesn't work. Can anyone tell why?
#include <iostream>
using namespace std;
int mean( int );
int main() {
int array[] = {43, 5, 3, 5, 2};
cout << mean(array);
}
int mean( int list[] ) {
return list[0];
}
These are the errors I'm getting:
Invalid conversion from 'int*' to 'int'
Initializing argument 1 of 'int mean(int)'

You are forward declaring the mean function using a different signature. Fix your forward declaration:
int mean( int[] );

You're missing the brakets in your prototype. Try this:
int mean(int[]);

Related

"No match for operator>" when using priority queue in C++

#include<bits/stdc++.h>
using namespace std;
struct vert
{
int n,m;
vert(int Node1=0,int Node2=0)
{
n=Node1,m=Node2;
}
bool operator<(vert const &obj)
{
return m<obj.m;
}
};
int main()
{
priority_queue<vert> q;
q.push(vert(1,2));
}
when I run this code, I get the following error, error: no match for 'operator<' (operand types are 'const vert' and 'const vert'), I even declare what the operator < does, but it still doesn't work, How do I fix this?
As the error message says, use const.
Try this
bool operator<(vert const &obj) const

error: redefinition of 'int main()' even if "int main()" apperas only once

I was working on a problem on Pbinfo: https://www.pbinfo.ro/probleme/898/sumfactcif but each time I tried to run my code it said:
sumfactcif.cpp: In function 'int main()':
sumfactcif.cpp:35:5: error: redefinition of 'int main()'
int main(){
^
sumfactcif.cpp:25:5: error: 'int main()' previously defined here
int main()
^
I don't know what to do because in my IDE(Codebloks) the code has no errors.
Here's the code if you can help me:
#include <iostream>
using namespace std;
int sumfactcif(int x)
{
int p,p1=0;
while(x>0)
{
int u=x%10;
p=1;
for(int i=1;i<=u;i++)
{
p=p*i;
}
p1=p1+p;
x=x/10;
}
return p1;
}
int main()
{
int x,fct;
cin>>x;
fct=sumfactcif(x);
cout<<fct;
}
Thanks!
Answer: Looks like the site already added an "int main" to my code, so that the result had two "int mains". Thanks to #churill for pointing that out

Error narrow conversion when used with auto

Getting error in use of auto for the implicit conversion.
Capturing the return of v.size() using int variable is ok but auto complains.
Compiler error does inform it is because of narrowing conversion .But I would like to understand in terms memory how this happens and why auto is not allowing it do that whereas normal conversion is ok
#include <iostream>
#include <vector>
int main()
{
auto v = std::vector<int>{ 1, 2, 3 };
auto c = 'h';
auto n2 = int{c};
std::cout<<n2<<" "<<c;
auto size = int{v.size()};
std::cout<<size;
int size_1 = v.size();
std::cout<<size_1;
}
Error because of below line
auto size = int{v.size()};
main.cpp: In function 'int main()': main.cpp:11:27: error: narrowing
conversion of 'v.std::vector::size()' from
'std::vector::size_type {aka long unsigned int}' to 'int' inside
{ } [-Wnarrowing]
When that line is commented it works perfectly
#include <iostream>
#include <vector>
int main()
{
auto v = std::vector<int>{ 1, 2, 3 };
auto c = 'h';
auto n2 = int{c};
std::cout<<n2<<" "<<c;
//auto size = int{v.size()};
//std::cout<<size;
int size_1 = v.size();
std::cout<<size_1;
}
Output
104 h3
DEMO LINK
This has nothing to do with auto. You will get the same warning if you use
int size = int{v.size()};
The issue here is int{v.size()} is using braced initialization to initialize a temporary int. A narrowing conversion in braced initialization is what causes the diagnostic to be displayed.
Do note that
int size_1 = v.size();
Is also a narrowing conversion, it is just not one where the standard mandates that a warning/error be emitted.

Variadic function int to size_t warning

I have a function accept multiple arguments.
#include <iostream>
#include <cstdarg>
#include <vector>
using namespace std;
template<typename... Values>
void doSomething(size_t input, Values... inputs)
{
size_t len = sizeof...(Values) + 1;
size_t vals[] = {input, inputs...};
vector<size_t> n(len);
std::copy( vals, vals+len, n.data() );
for(size_t i=0; i<len; i++) cout<<n[i]<<endl;
//Do something with n vector
}
It works fine when I call this function by:
size_t a(1), b(2), c(3);
doSomething(a,b,c);
However, it will have a problem when I call this function by:
doSomething(1,2,3);
It will give out warning message:
warning: narrowing conversion of ‘inputs#0’ from ‘int’ to ‘size_t {aka long unsigned int}’ inside { } [-Wnarrowing]
size_t vals[] = {inputs...};
I do not like this warning message, is there a way to solve this problem? I would like the function to be able to accept either size_t or int. Thank you.

c-style string sorting with sort and qsort

I'm trying to use both sort and qsort to sort a c-style string and them see which of them is better, so I've written this code, but it is not working , so can you please tell me what is wrong with it.
thanks in advance.
#include <iostream>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<chrono>
#include<string>
#include<sstream>
using namespace std;
using namespace std::chrono;
void bvect(vector<double> &vec, int num)
{
auto gen = bind(normal_distribution<double>(15,4.0),default_random_engine());
for(int i=0; i<num; ++i)
vec.push_back(gen());
}
char* converttostring(int number)
{
stringstream ss;
ss << number;
return (ss.c_str());
}
int cst_cmp(const void *one, const void *two)
{
char a = *((char*)one);
char b = *((char*)two);
return strcmp(a, b);
}
//Generated random strings
void textvect(vector<string> &vec, int num)
{
srand(time(NULL));
for(int i=0; i<num; ++i)
vec.push_back(converttostring(rand()%num +1));
}
void displayvector(vector<char*>vect)
{
for (int i=0; i<vect.size(); ++i){
for (int j=0; j<strlen(vect[i]); ++j)
cout<<vect[i][j];
cout<<endl;
}
}
int main(){
int sz=100000;
vector<char*>text1, text2;
textvect(text1, sz);
text2.resize(text1.size());
copy(text1.begin(),text1.end(),text2.begin());
// qsort() string
auto t1 = system_clock::now();
qsort(&text1[0], text1.size(), sizeof(char*), cst_cmp);
auto t2 = system_clock::now();
auto dms = duration_cast<milliseconds>(t2-t1);
cout << "string qsort() took " << dms.count() << " milliseconds\n";
// sort() string
auto t3 = system_clock::now();
std::sort(text2.begin(), text2.end());
auto t4 = system_clock::now();
auto dms1 = duration_cast<milliseconds>(t4-t3);
cout << "string sort() took " << dms1.count() << " milliseconds\n";
return 0;
}
For std::sort, you are just using the default comparator, which will just compare pointer values. You need to pass a comparator that does a proper comparison (using strcmp, for example):
std::sort(text2.begin(), text2.end(),
[](const char* lhs, const char* rhs) { return strcmp(lhs,rhs) < 0; });
That's one problem, there may be others.
One problem is in your compare function for qsort:
int cst_cmp(const void *one, const void *two)
{
char a = *((char*)one);
char b = *((char*)two);
return strcmp(a, b);
}
You are not comparing strings here, because a and b are just chars. You might as well avoid them:
int cst_cmp(const void *one, const void *two)
{
return (strcmp(*(char **)one, *(char **)two));
}
These are the errors I obtain trying to compile your code:
> g++ main.cc -std=c++0x
main.cc: In function ‘char* converttostring(int)’:
main.cc:24:15: error: ‘std::stringstream’ has no member named ‘c_str’
main.cc: In function ‘int cst_cmp(const void*, const void*)’:
main.cc:31:23: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
/usr/include/string.h:143:12: error: initializing argument 1 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
main.cc:31:23: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
/usr/include/string.h:143:12: error: initializing argument 2 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
main.cc: In function ‘int main()’:
main.cc:55:23: error: invalid initialization of reference of type ‘std::vector<std::basic_string<char> >&’ from expression of type ‘std::vector<char*>’
main.cc:35:6: error: in passing argument 1 of ‘void textvect(std::vector<std::basic_string<char> >&, int)’
24:15 c_str() is a member function of string not of stringstream. See here.
31:23 strcmp() wants two const char * not two char. See here.
55:23 and 35:6 char* is not the same type as string.
This function isn't working
char* converttostring(int number)
{
   stringstream ss;
   ss << number;
   return (ss.c_str());
}
and if it was sort of fixed (ss.str().c_str()), it would return a pointer to a temporary.
If you have a compiler with some C++11 support, you can use std::to_string from the standard library. Otherwise, change the return type to std::string (no pointer!).
Ask Stroustrup ;) just allocate space for the C string array and enter characters ino it.. remember to deallocate it..