I have written a simple program.
I am getting this error:
time.cpp: In function ‘int main()’:
time.cpp:22:9: error: expected ‘;’ before ‘a’
time.cpp:23:4: error: ‘a’ was not declared in this scope
time.cpp:24:4: error: ‘b’ was not declared in this scope
time.cpp:25:4: error: ‘c’ was not declared in this scope
This is my code:
#include<iostream>
using namespace std;
class time
{
int hour;
int min;
public:
void gettime(int h,int m)
{
hour=h;
min=m;
}
void puttime()
{
cout<<hour<<endl<<min;
}
void sum(time x,time y)
{
min=x.min+y.min;
hour=min/60;
min=min%60;
hour=hour+x.hour+y.hour;
}
};
int main()
{
time a,b,c;
a.gettime(2,45);
b.gettime(3,35);
c.sum(a,b);
a.puttime();
b.putime();
c.puttime();
return 0;
}
Remember that there is a standard function named time.
This is the one main reason you should refrain from using namespace std;.
b.putime() must be b.puttime() here. Otherwise this code compiled
Related
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
In nestClassDef.h I have written code like this
class A{
public:
class B{
public:
void BTest();
};
};
class B{
};
then in the nestClassDef.cpp I am writing code like this
#include "nestClassDef.h"
#include<iostream>
void A::B::BTest(){
cout<<"Hello World!";
}
int main(){
A a;
A.B b;
b.BTest();
}
But when I am compiling the above code
g++ -o nestClassDef nestClassDef.cpp
I am getting error like this :-
nestClassDef.cpp: In member function ‘void A::B::BTest()’:
nestClassDef.cpp:5: error: ‘cout’ was not declared in this scope
nestClassDef.cpp: In function ‘int main()’:
nestClassDef.cpp:10: error: expected unqualified-id before ‘.’ token
nestClassDef.cpp:11: error: ‘b’ was not declared in this scope
I am at a loss how to fix this. Any understanding shared will be thankfully received.
For the cout error: it's in std namespace, so use std::cout.
For The second error: B is not A's member, it's a nested type, so you have to use A::B b;
nestClassDef.cpp: In member function ‘void A::B::BTest()’:
nestClassDef.cpp:5: error: ‘cout’ was not declared in this scope
Use std::cout instead of cout, or add using namespace std; (probably after your #include statements).
nestClassDef.cpp: In function ‘int main()’:
nestClassDef.cpp:10: error: expected unqualified-id before ‘.’ token
nestClassDef.cpp:11: error: ‘b’ was not declared in this scope
Use A::B instead of A.B.
Add using namespace std; or use std::cout instead of just cout
Use A::B not A.B. Dot operator is used on objects or structs/unions.
From http://cppcms.com/wikipp/en/page/cppcms_1x_tut_hello_templates#The.controller
I've places below codes on bottom of hello.cpp:
virtual void main(std::string /*url*/)
{
content::message c;
c.text=">>>Hello<<<";
render("message",c);
}
When running g++ hello.cpp my_skin.cpp -o hello -lcppcms -lbooster, got error:
hello.cpp:44:38: error: ‘virtual’ outside class declaration
hello.cpp:44:38: error: ‘::main’ must return ‘int’
hello.cpp:44:14: warning: first argument of ‘int main(std::string)’ should be ‘int’ [-Wmain]
hello.cpp:44:14: warning: ‘int main(std::string)’ takes only zero or two arguments [-Wmain]
hello.cpp: In function ‘int main(std::string)’:
hello.cpp:44:38: error: declaration of C function ‘int main(std::string)’ conflicts with
hello.cpp:27:5: error: previous declaration ‘int main(int, char**)’ here
hello.cpp: In function ‘int main(std::string)’:
hello.cpp:48:23: error: ‘render’ was not declared in this scope
Do I missed something
The error messages are telling you everything you need to know.
virtual can only be used in a class. Your main method is not in a class.
The main method must return an int. Yours is returning void.
You have two main methods, one that is main(std::string) and one that is main(int, char**)
Your render method must have a function prototype above the main method or the entire method needs to me moved.
So this would be more appropriate:
void render(std::string, std::string) // or whatever
{
// do something
}
int main(int argc, char** argv)
{
render("string", c);
return 0;
}
Your hello.cpp should look like below:
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <iostream>
#include "content.h"
class hello : public cppcms::application {
public:
hello(cppcms::service &srv) : cppcms::application(srv) {}
virtual void main(std::string url);
};
void hello::main(std::string /*url*/){
content::message cc;
cc.text=">>>Hello<<<";
render("message", cc);
}
int main(int argc,char ** argv){
try {
cppcms::service srv(argc,argv);
srv.applications_pool().mount(cppcms::applications_factory<hello>());
srv.run();
}
catch(std::exception const &e) {
std::cerr<<e.what()<<std::endl;
}
}
(Beginner programmer..) I'm following the style of a header file that worked fine, but I'm trying to figure out how I keep getting all of these errors when I compile. I am compiling with g++ in Cygwin.
Ingredient.h:8:13: error: expected unqualified-id before ‘)’ token
Ingredient.h:9:25: error: expected ‘)’ before ‘n’
Ingredient.h:19:15: error: declaration of ‘std::string <anonymous class>::name’
Ingredient.h:12:14: error: conflicts with previous declaration ‘std::string<anonymous class>::name()’
Ingredient.h:20:7: error: declaration of ‘int <anonymous class>::quantity’
Ingredient.h:13:6: error: conflicts with previous declaration ‘int<anonymous class>::quantity()’
Ingredient.h: In member function ‘std::string<anonymous class>::name()’:
Ingredient.h:12:30: error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type ‘std::string’ requested
Ingredient.h: In member function ‘int<anonymous class>::quantity()’:
Ingredient.h:13:25: error: argument of type ‘int (<anonymous class>::)()’ does not match ‘int’
Ingredient.h: At global scope:
Ingredient.h:4:18: error: an anonymous struct cannot have function members
Ingredient.h:21:2: error: abstract declarator ‘<anonymous class>’ used as declaration
And here is my class header file...
#ifndef Ingredient
#define Ingredient
class Ingredient {
public:
// constructor
Ingredient() : name(""), quantity(0) {}
Ingredient(std::string n, int q) : name(n), quantity(q) {}
// accessors
std::string name() { return name; }
int quantity() {return quantity; }
// modifier
private:
// representation
std::string name;
int quantity;
};
#endif
I am confused by these errors and don't really know what I am doing wrong concerning the implementation of the class..
That's a funny one. You are essentially killing your class name by #define Ingredient - all occurrences of Ingredient will be erased. This is why include guards generally take the form of #define INGREDIENT_H.
You are also using name both for the member and the getter function (probably an attempt to translate C#?). This is not allowed in C++.
How about look on errors? variables and functions can't have same names. And include guard should never names such as class.
#ifndef INGREDIENT_H
#define INGREDIENT_H
class Ingredient {
public:
// constructor
Ingredient() : name(""), quantity(0) {}
Ingredient(std::string n, int q) : name(n), quantity(q) {}
// accessors
std::string get_name() const { return name; }
int get_quantity() const {return quantity; }
// modifier
private:
// representation
std::string name;
int quantity;
};
#endif
I have started working on C++ language.I am very new to it.The program takes a complex number from the user and prints it out.But its giving me many errors like,
prog.cpp: In function ‘int main()’:
prog.cpp:26: error: ‘GetReal’ was not declared in this scope
prog.cpp:26: error: ‘SetReal’ was not declared in this scope
prog.cpp:27: error: ‘GetImag’ was not declared in this scope
prog.cpp:27: error: ‘SetImag’ was not declared in this scope
prog.cpp:28: error: ‘print’ was not declared in this scope
prog.cpp: At global scope:
prog.cpp:34: error: expected unqualified-id before ‘)’ token
prog.cpp:40: error: expected unqualified-id before ‘float’
prog.cpp:40: error: expected `)' before ‘float’
prog.cpp: In function ‘void SetImag(float)’:
prog.cpp:64: error: ‘real’ was not declared in this scope
prog.cpp: In function ‘void SetReal(float)’:
prog.cpp:68: error: ‘imag’ was not declared in this scope
prog.cpp: In function ‘void print()’:
prog.cpp:73: error: ‘Cout’ was not declared in this scope
prog.cpp:73: error: ‘real’ was not declared in this scope
prog.cpp:73: error: ‘imag’ was not declared in this scope
Here's the code:
/*
Date=13 January 2011
Program: To take a complex number from the user and print it on the screen */
/*Defining a class*/
#include <iostream>
using namespace std;
class complex
{
float real;
float imag;
public:
complex();
complex(float a,float b);
float GetReal();
float GetImag();
void SetReal();
void SetImag();
void print();
};
int main()
{
complex comp;
SetReal(GetReal());
SetImag(GetImag());
print();
}
complex()
{
real=0;
imag=0;
}
complex(float a,float b)
{
real=a;
imag=b;
}
float GetReal()
{
float realdata;
cout<<"Enter Real part:"<<endl;
cin>>realdata;
return realdata;
}
float GetImag()
{
float imagdata;
cout<<"Enter Imaginary part"<<endl;
cin>>imagdata;
return imagdata;
}
void SetImag(float a)
{
real=a;
}
void SetReal(float b)
{
imag=b;
}
void print()
{
printf("The Complex number is %f+%fi",real,imag);
}
Since GetReal() et al are declared as part of the complex class, you should call them on the object you created:
complex comp;
comp.SetReal(comp.GetReal());
comp.SetImag(comp.GetImag());
comp.print();
Similarly, you need to scope the implementation of the complex constructor:
complex::complex()
{
real=0;
imag=0;
}
The same applies to the other member functions not shown in your post.
In your main function you need to call GetReal and SetReal on an instance of the class:
e.g.
Complex comp;
comp.SetReal();
...
Also, your method bodies aren't bound to the class, they're floating in the global namespace. You need to define them:
void Complex::SetReal() {} //etc
Hope this helps