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
I just recently started messing around with separate class files in c++ and this was my first attempt:
First I made a class header called "ThisClass.h":
//ThisClass.h
#ifndef THISCLASS_H
#define THISCLASS_H
class ThisClass
{
private:
int x;
float y;
public:
ThisClass(int x, float y);
void setValues(int x, float y);
int printX();
float printY();
};
#endif // THISCLASS_H
Then, I implemented my class in a file called "ThisClass.cpp":
//ThisClass.cpp
#include "ThisClass.h"
ThisClass::ThisClass(int x, float y)
{
this->x = x;
this->y = y;
}
void ThisClass::setValues(int x, float y)
{
this->x = x;
this->y = y;
}
int ThisClass::printX()
{
return this->x;
}
float ThisClass::printY()
{
return this->y;
}
Finally, I made a file called "main.cpp" where I used the class:
//main.cpp
#include <iostream>
using namespace std;
int main()
{
ThisClass thing(3, 5.5);
cout << thing.printX() << " " << thing.printY()<< endl;
thing.setValues(5,3.3);
cout << thing.printX() << " " << thing.printY()<< endl;
return 0;
}
I then compiled and ran this program through Code Blocks which uses the MinGW compiler and received the following errors:
In function 'int main()':|
main.cpp|7|error: 'ThisClass' was not declared in this scope|
main.cpp|7|error: expected ';' before 'thing'|
main.cpp|8|error: 'thing' was not declared in this scope|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Am I somehow doing this wrong? Any help would be appreciated.
You forgot to #include "ThisClass.h" in main.cpp.
As already answered that you forgot to put #include "ThisClass.h"in main.cpp.
Just do that and your code will be compiled.
I just want to answer your question -
However, now my console isn't outputting anything even though I have 2 cout calls
Please put a getchar() before return in main function, it will allow you to see your output.
My task is
Write a subprogram that calculates n!. While using this subprogram make a program that calculates (a+b)!.
I wrote the following code:
using namespace std;
int f(int n) {
for(int i=1;i<=n;i++){
f=f*i;
}
return f;
}
int main() {
int a,b;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
cout<<"factorial of a+b="<<(a+b)*f;
return 0;
}
And I get this error when I compile:
In function 'int f(int)':
7:27: error: invalid operands of types 'int(int)' and 'int' to binary 'operator*'
8:8: error: invalid conversion from 'int (*)(int)' to 'int' [-fpermissive]
In function 'int main()':
16:34: error: invalid operands of types 'int' and 'int(int)' to binary 'operator*'
17:9: error: expected '}' at end of input
Here's your error
return f;
returns a function pointer of your function f(). Also you're using the function pointer for your calculations.
In C++ you need to declare a variable that can be used to calculate and return:
int f(int n) {
int result = 1;
for(int i=1;i<=n;i++) {
result=result*i;
}
return result;
}
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 8 years ago.
Improve this question
This is a fairly simple program for the implementation of Stack. But there is a problem with the declaration of the push(), pop() and display() functions. The error statements are as follows. Please help me figure out the problem.
Error:
error C2664: 'push' : cannot convert parameter 1 from 'int [10]' to 'int'
error C2664: 'pop' : cannot convert parameter 1 from 'int [10]' to 'int'
error C2664: 'display' : cannot convert parameter 1 from 'int [10]' to 'int'
Program:
#include<iostream>
#define STACKSIZE 10
using namespace std;
void push(int,int,int);
void pop(int,int);
void display(int,int);
int main()
{
int stack[STACKSIZE],n,data,stackptr=-1;
while(1)
{
cout<<"1. Push\n2. Pop\n3. Display\n4. Exit\n";
cin>>n;
switch(n)
{
case 1:
cout<<"\nEnter a data to push: ";
cin>>data;
push(stack,stackptr,data);
break;
case 2:
pop(stack, stackptr);
break;
case 3:
display(stack, stackptr);
break;
case 4:
exit(1);
break;
default:
cout<<"\nEnter correct choice...\n\n";
}
}
system("pause");
return 0;
}
void display(int stack[STACKSIZE], int &stackptr)
{
cout<<"\n\n";
for(int i=0;i<stackptr;i++)
cout<<stack[i]<<"\n";
cout<<"\n\n";
}
void push(int stack[STACKSIZE],int &stackptr, int data)
{
if(stackptr == STACKSIZE - 1)
{
cout<<"\n\nStack full\n\n";
return ;
}
stackptr++;
stack[stackptr] = data;
}
void pop(int stack[STACKSIZE], int &stackptr)
{
if(stackptr == 0)
{
cout<<"\n\nStack Empty\n\n";
return ;
}
stackptr = stackptr-1;
}
The answer given by Gangadhar and Jansel is incomplete. You are passing values by reference so this is not enough:
void push(int stack[size],int,int);
This is what you need:
void push(int[],int&,int);
void pop(int[],int&);
void display(int[],int&);
Your function's declarations are wrong. for example:
void push(int, int, int);
// ^^^ int
Just correct it
void push(int stack[], int&, int);
// ^^^^^^^^^^^ should be int[]
You were making mistake in function declaration. This is the correct way.
void push(int[],int&,int);
void pop(int[],int&);
void display(int[],int&);
These error are self explanatory push,pop and display functions prototypes receives 3 int parameters, and you are trying to pass int[] by first first parameter, so, change your functions prototype to receive int[] of it first parameter.
You have a function prototype for push/pop at the start that declares an int not an array.
void push(int, int, int);
This is the version the compiler uses to check your call against. Rewrite these prototypes to have the function signature as the functions themselves.
I receive the errors:
cs163hw1.cpp:41:24: error: no ‘int menutype::run_prog()’ member function declared in class ‘menutype’
and
main.cpp:18:7: error: ‘struct menutype’ has no member named ‘run_prog’
When attempting to compile my program with the associated code (spanning the appriprait .cpp and .h files):
int main(int argc, char ** argv){
...
menu.run_prog();
...
class menutype{
public:
menutype(int);
int display();
int run_prog();
private:
extras list;
person menup;
};
int menutype::run_prog(){
bool exit = false;
int input;
while(!exit){
input = 0;
while(input < 1 || input > 4)
input = display();
switch(input){
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 : exit = true;
break;
default :
break;
}
}
}
I have no idea why this is happening, any guesses?
You need to have the class menutype declared above main(). Better still is to move the class to its own dedicated file called menutype.cpp and include the header in main's source file. As you have described it, the complier has no knowledge of menutype yet as its parsing the source file from the top of the file.