CodeBloks C++ Error: Was not declared in the scope - c++

Hey guys I'm trying to learn C++ and I was doing pretty well until I hit this wall..
I am getting two errors:
error: 'enter' was not declared in this scope
error: 'Satisfies' was not declared in this scope|
Here is my file. Why is that?
include <iostream>
using namespace std;
int main() {
while (1){
char menu;
cin>>menu;
switch (menu){
case 1: Enter(); break;
case 2: Satisfies(); break;
case 3: break;
};
};
}
int Enter(){
return 0;
}
int Satisfies(){
return 0;
}

You have to declare the functions before their usage for example before main and use the correct syntax of calling functions.
For example
//...
int Enter(){ return 0; }
int Satisfies(){ return 0; }
//...
int main()
{
//...
case 1:
Enter();
//...

You need to declare the functions before they are used. So put this above main:
int Enter();
int Satisfies();
You can leave the definitions (the bit that actually contains the code to run when the function is called) where they are. Or you can just move those functions above main, since a function definition is also a declaration.
The compiler needs to see these bits before it tries to call the function, so that it can know what arguments it needs, and what will be returned.
See this question.

You need to declare (or declare and definite) a function before you use it. And you need to make function calls with (). Like Enter() and Satisfied(). If you want to learn good programming and C coding and then go to C++ read "C for Dummies" by Dan Godkin. My favourite coding book.
You have 3 ways to do this and fix your code:
1. Write prototy definitions:
#include <iostram>
int Enter();
int Satisfies();
using namespace std;
int main()
{
//bla
}
int Enter(){ return 0; }
int Satisfies(){ return 0; }
2. Make a function.h file and put the declarations there. Save it in the same folder as the c / cpp file
then include in your code
#include "function.h"
3 Put your functions in order of execution in the c/cpp file. A function must be declared before it is used. Example:
void Enter()
{
//bla
}
void Satisfied()
{
//blub
}
int main()
{
Enter();
Satisfied();
}
More tricky example, when a function (Satisfied) uses an other function (Enter) the Enter function must be declared before the Satisfied function:
void Enter()
{
//bla
}
void Satisfied()
{
//blubb
Enter(); //now Enter must be declared before is Satisfied() is defined, so it must be "over" it in the source like in this example
}
int main()
{
Enter();
Satisfied();
}

Function call need to have () after the function name doesn't matter whether it take any parameter or not. Plus you need to define function header before main()
include <iostream>
using namespace std;
int Enter();
int Satisfies();
int main() {
while (1){
char menu;
cin>>menu;
switch (menu){
case 1: Enter();
break;
case 2: Satisfies();
break;
case 3:
break;
};
};
}
int Enter(){
return 0;
}
int Satisfies(){
return 0;
}

Related

C++ error: invalid use of 'AppleFarmer::AppleFarmer' when ca

I am getting the error "error: Invalid use of AppleFarmer::AppleFarmer. I do not know why I am getting this error since I am not trying to pass any input into my Constructor. Is it possible I have an issue with my .h file? What am i doing wrong to get this error?
I have three different files, and I may also be having an issue with linking the code together as I am doing #include for a .cpp file. I am not sure if my code works aside from this error, but I am stuck on this error.
appleFarmerMain.cpp
#include<iostream>
#include "appleFarmer.cpp"
int main(){
AppleFarmer m;
int harvest;
int demand;
m.AppleFarmer();
while(m.endOfMonth()==false){
cout<<"Enter a harvest amount:"<<endl;
cin>>harvest;
m.harvestApples(harvest);
cout<<"Enter a demand:"<<endl;
cin>>demand;
m.sellApples(demand);
cout<<"Apple Inventory: "<<m.getInventory()<<endl;
m.updateCurrentDay();
}
return 0;
}
appleFarmer.cpp
#include "appleFarmer.h"
#include "<iostream>
using namespace std;
AppleFarmer::AppleFarmer(){
for(int i=0;i<30;i++){
sales[i]=0;
harvest[i]=0;
}
}
bool AppleFarmer::sellApples(int demand){
if(demand<= inventory){
sales[currentDay]=demand;
inventory=inventory-demand;
}
else{
sales[currentDay]=0;
}
}
void AppleFarmer::harvestApples(int dayHarvest){
harvest[currentDay]= dayHarvest;
inventory=inventory+dayHarvest;
}
bool AppleFarmer::endOfMonth(){
if (currentDay=maxDays){
return true;
}
else{
return false;
}
}
int AppleFarmer::updateCurrentDay(){
currentDay=currentDay+1;
}
int AppleFarmer::getInventory(){
return inventory;
}
double AppleFarmer::calculateAverageHarvest(){
}
double calculateAverageSales(){
}
void AppleFarmer::printSales(){
}
void AppleFarmer::printHarvest(){
}
appleFarmer.h
#ifndef APPLEFARMER_H
#define APPLEFARMER_H
class AppleFarmer
{
public:
AppleFarmer();
bool sellApples(int);
void harvestApples(int);
bool endOfMonth();
int updateCurrentDay();
int getInventory();
double calculateAverageHarvest();
double calculateAverageSales();
void printSales();
void printHarvest();
private:
int sales[30];
int harvest[30];
int maxDays = 30;
int currentDay = 0;
int inventory = 0;
};
#endif
In C++ you don't call the constructor on an object. That happens at object creation time. The line
m.AppleFarmer();
isn't needed. The constructor is implicitly called here:
AppleFarmer m;
You need to include appleFarmer.h instead of appleFarmer.cpp because the header file (with .h extension) contains the declaration while the .cpp file contains the implementation.
Then you need also to delete m.AppleFarmer(); because the constructor is called during the declaration (AppleFarmer m text line).

Obtain variables in function from other function C++

I am trying to pass a variable from one function to another. I have tried this approach but it is not working for me:
int c (){
int x1,x2,y2,y1;
system("cls");
cout<<"Insert Value"<<endl
cin>>x1;
return x1;
}
int cd()
{
int a;
a=c();
cout<<"X1: "<<a;
}
Any help is appreciated. Thanks!
There are a few problems with your code.
First of all you are missing a semicolon after the cout statement in your c() function.
Also you have indicated that function cd() should return an int but you are not returning anything.
Lastly, these function will not begin execution unless you call them explicitly.
Try this:
#include <iostream>
using namespace std;
int c (){
int x1,x2,y2,y1;
cout<<"Insert Value"<<endl;
cin>>x1;
return x1;
}
int cd(){
int a;
a=c();
cout<<"X1: "<<a;
return a;
}
int main()
{
int x=cd(); //call the function to create the side effects
return 0;
}

void() issue - It doesn't print results

I am writing a program split in three different files:
1) An header named my.h;
A source cpp file named my.cpp;
The main file named use.cpp;
Here their statements:
/* Header file my.h
Define global variable foo and functions print and print_foo
to print results out */
extern int foo;
void print_foo();
void print(int);
/* Source file my.cpp where are defined the two funcionts print_foo() and print()
and in where it is called the library std_lib_facilities.h */
#include "stdafx.h"
#include "std_lib_facilities.h"
#include "my.h"
void print_foo() {
cout << "The value of foo is: " << foo << endl;
return;
}
void print(int i) {
cout << "The value of i is: " << i << endl;
return;
}
/ use.cpp : definisce il punto di ingresso dell'applicazione console.
//
#include "stdafx.h"
#include "my.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int foo = 7;
int& i = foo;
i = 99;
char cc = '0';
while (cin >> cc) {
switch (cc) {
case '1':
void print_foo();
break;
case '2':
void print();
break;
default:
exit(EXIT_FAILURE);
}
}
return 0;
}
My main problem is that program compiles and run correctly but it doesn't print anything as I supposed.
How can I fix it?
Thank you!
Leo
To call a function specifying return type is not required. Correct
void print_foo(); // This actually declares a function prototype
to
print_foo();
and
print(i); // Pass i as argument
Drop the void from void print_foo(); and void print(); in the switch blocks.
Currently you're just declaring a function prototype; not actually calling the function.
Your extern int foo; approach, while syntactically valid, can make your codebase harder to scale and maintain: consider passing the parameter explicitly.
Your code claims to *"Define global variable foo..." as follows...
extern int foo;
...but that just declares that some translation unit will actually define it (without the leading extern qualifier). There's no actual variable in the code you've posted, which means your program shouldn't link unless some library you're using coincidentally has a foo symbol in it.
This shorter code condenses your problem:
#include <iostream>
extern int foo;
void f()
{
std::cout << foo << '\n';
}
int main() {
int foo = 7;
f();
}
You can see the compiler error message here, namely:
/tmp/ccZeGqgN.o: In function `f()':
main.cpp:(.text+0x6): undefined reference to `foo'
collect2: error: ld returned 1 exit status
You would put in the main function
case '1':
print_foo();
break;
Notice I erased the word "void" in the case 1. Because you don't redeclare the function there. You just use it.

Proper use of namespaces for function definitions in cpp file

So for some reason I have experienced the behavior that adding a namespace to my .h and .cpp files for a set of functions breaks my linker. I'm using Visual Studio 2012. Here's my scenario (simplified)
functions.h
int functionA();
int functionB();
functions.cpp
#include "functions.h"
int functionA() { return 0; }//we could pretend there's actual code here
int functionB() { return 0; }//we could pretend there's actual code here
and the actual useage of it is in some cpp file thusly:
pointers.h
#include "functions.h"
class GetPointers
{
public:
typedef int (*FunctionPointer)(void);
static FunctionPointer funcPointerA() { return &functionA; }
static FunctionPointer funcPointerB() { return &functionB; }
};
Well that's all fine and dandy. I can call the static method of GetPointers and get a function pointer which works. Everythings been tested and everything is happy. Now I thought I would simply add some namespaces to make sure I don't have any problems again in the future. So I simply modify the three code files to use namespaces. What happens is a link error which refers to the function funcPointerA() and funcPointerB() of the GetPointers class, with the full namespace name to functionA and functionB.
functions.h
namespace fun {
int functionA();
int functionB();
}
functions.cpp
#include "functions.h"
using namespace fun;
int functionA() { return 0; }//we could pretend there's actual code here
int functionB() { return 0; }//we could pretend there's actual code here
and the actual useage of it is in some cpp file thusly:
pointers.h
#include "functions.h"
namespace fun {
class GetPointers
{
public:
typedef int (*FunctionPointer)(void);
static FunctionPointer funcPointerA() { return &functionA; }
static FunctionPointer funcPointerB() { return &functionB; }
};
}
I don't get a build error, only a link error about fun::functionA and fun::functionB. Is there something implicitly wrong with using function pointers from namespaces?
The problem is that your definitions:
int functionA() { return 0; }
int functionB() { return 0; }
are in the global namespace; so they declare new functions there rather than define the functions declared in namespace fun.
The best fix is to qualify the names in the definitions:
int fun::functionA() { return 0; }
int fun::functionB() { return 0; }
This is preferable to putting the definitions inside the namespace, since it gives a compile-time check that the functions match their declarations.

C++ functions problem

Is there a way for functions to call each other i.e.
void menu()
{
some code here
play();
...
}
int play()
{
...
menu();
...
return 0;
}
Add the declaration of the second function at the top of your code file:
int play();
void menu()
{
// some code here
play();
// ...
}
int play()
{
// ...
menu();
// ...
return 0;
}
This is called a forward declaration, and it informs the compiler that an identifier will be declared later.
It is a way of denoting a function so that you can call it before you provide the complete definition.
Yes, but this is almost never what you want to do since careless use will break the stack.