error: '' was not declared in this scope - c++

I was trying to do a simple program to learn a bit more about reference passage, pointers and how to use this 2 on structs in C++ and I got a few questions.
I got an error on the code below "error: 'totalStudent' was not declared in this scope" and my question is, how should I declare the "totalStudent".
#include <iostream>
using namespace std;
struct test{
char name[30];
int age;
};
void addStudent(struct test *ptrTest,int *totalStudent){
for(int i=0;i<2;i++){
cout<<"\nInsert the name: ";
cin.sync();
cin.getline(ptrTest->name,sizeof(ptrTest->name));
cout<<"\nInsert the age: ";
cin.sync();
cin>>ptrTest->age;
*totalStudent+=1;
}
}
void showStudent(struct test *ptrTest,int totalStudent){
for(int i=0;i<totalStudent;i++){
cout<<"\nName: "<<ptrTest->name;
cout<<"\nAge: "<<ptrTest->age;
}
};
int main()
{
struct test t;
addStudent(&t,&totalStudent);
showStudent(&t,totalStudent);
return 0;
}
I can't use pointers and reference passages with structures very well. I can only use them when I'm not using structs.

You forgot to declare this variable in the scope of main:
int main()
{
struct test t;
// LIKE THIS
int totalStudent;
addStudent(&t,&totalStudent);
showStudent(&t,totalStudent);
return 0;
}

Related

‘sortBook’ was not declared in this scope [duplicate]

This question already has answers here:
Sorting a vector of custom objects
(14 answers)
Closed 1 year ago.
I cannot solve the error - main.cpp:31:34: error: ‘sortBook’ was not declared in this scope in the line sortBook(arr,arr+n,comparator);
I apologize for asking a question that should have a simple solution, but it's driving me nuts. I've checked for all the common errors.
#include<iostream>
using namespace std;
class BOOK{
private:
char bookname[20];
float bookprice;
public:
void getBookDetails()
{
cout<<"Enter the Book Name:";
cin>>bookname;
cout<<"Enter the Book Price:";
cin>>bookprice;
}
void displayDetails()
{
cout<<"Book Name:"<<bookname<<endl;
cout<<"Book Price:"<<bookprice<<endl;
}
};
bool comparator(string a,string b)
{
return a<b;
}
int main()
{
int n=5;
string arr[]={"sandwich","apple","banana","zombie","pear"};
sortBook(arr,arr+n,comparator);
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
I think it's just that you have not declared sortBook function anywhere and trying to use it.
Your error shows up because you have not defined the method "sortBook".
There are 2 ways to fix this error:
Define your own "sortBook" method in your class.
Use C++ sort method that is defined in "Algorithm". Below is my code that uses this sort method. You have to "include algorithm " at the top of your file, and call the sort method.
#include<iostream>
#include<algorithm> ////// YOU SHOULD INCLUDE THIS STATEMENT
using namespace std;
class BOOK{
private:
char bookname[20];
float bookprice;
public:
void getBookDetails()
{
cout<<"Enter the Book Name:";
cin>>bookname;
cout<<"Enter the Book Price:";
cin>>bookprice;
}
void displayDetails()
{
cout<<"Book Name:"<<bookname<<endl;
cout<<"Book Price:"<<bookprice<<endl;
}
};
bool comparator(string a,string b)
{
return a<b;
}
int main()
{
int n=5;
string arr[]={"sandwich","apple","banana","zombie","pear"};
std::sort(arr,arr+n,comparator); // HERE, YOU CALL : sort()
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
Note: My code above compiles with no errors. It runs and produces the correct output. Please let me know if you have any other issues.

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;
}

Instance of class only allows 1 method, or program crashes

I am learning classes and OOP, so I was doing some practice programs, when I came across the weirdest bug ever while programming.
So, I have the following files, beginning by my class "pessoa", located in pessoa.h:
#pragma once
#include <string>
#include <iostream>
using namespace std;
class pessoa {
public:
//constructor (nome do aluno, data de nascimento)
pessoa(string newname="asffaf", unsigned int newdate=1996): name(newname), DataN(newdate){};
void SetName(string a); //set name
void SetBornDate(unsigned int ); //nascimento
string GetName(); //get name
unsigned int GetBornDate();
virtual void Print(){}; // print
private:
string name; //nome
unsigned int DataN; //data de nascimento
};
Whose functions are defined in pessoa.cpp
#include "pessoa.h"
string pessoa::GetName ()
{
return name;
}
void pessoa::SetName(string a)
{
name = a;
}
unsigned int pessoa::GetBornDate()
{
return DataN;
}
void pessoa::SetBornDate(unsigned int n)
{
DataN=n;
}
A function, DoArray, declared in DoArray.h, and defined in the file DoArray.cpp:
pessoa** DoArray(int n)
{
pessoa* p= new pessoa[n];
pessoa** pointer= &p;
return pointer;
}
And the main file:
#include <string>
#include <iostream>
#include "pessoa.h"
#include "DoArray.h"
#include <cstdio>
using namespace std;
int main()
{
//pessoa P[10];
//cout << P[5].GetBornDate();
pessoa** a=DoArray(5);
cerr << endl << a[0][3].GetBornDate() << endl;
cerr << endl << a[0][3].GetName() << endl;
return 0;
}
The weird find is, if I comment one of the methods above, "GetBornDate" or GetName, and run, the non-commented method will run fine and as supposed. However, if both are not commented, then the first will run and the program will crash before the 2nd method.
Sorry for the long post.
Let's look into this function:
int *get()
{
int i = 0;
return &i;
}
what is the problem with it? It is returning pointer to a local variable, which does not exist anymore when function get() terminates ie it returns dangling pointer. Now your code:
pessoa** DoArray(int n)
{
pessoa* p= new pessoa[n];
return &p;
}
do you see the problem?
To clarify even more:
typedef pessoa * pessoa_ptr;
pessoa_ptr* DoArray(int n)
{
pessoa_ptr p= whatever;
return &p;
}
you need to understand that whatever you assign to p does not change lifetime of p itself. Pointer is the same variable as others.

Error Code Debugging for C++ CodeBlocks

I keep getting an error message in CodeBlocks it says:
Error: 'Addnumbers' was not declared in this scope
I just started with C++ and have no idea what this means. Here is my code:
#include <iostream>
using namespace std;
int main()
{
int fnum;
int snum;
cout << "Enter First number" << endl;
cin >> fnum;
cout << "Enter Second Number" << endl;
cin >> snum;
Addnumbers (fnum, snum);
return 0;
}
int Addnumbers(int fnum, int snum){
int ans = fnum+snum;
return ans;
}
You need to declare the function before it's used:
int Addnumbers(int fnum, int snum);
int main()
{
}
int Addnumbers(int fnum, int snum)
{
// ...
}
The first declaration is what is called a prototype, and tells the compiler that somewhere there is a function named AddNumbers with the specified arguments and return type. Then you can have the definition anywhere, even in another source file.
In C++ (as well as in C or other languages base on C) everything must be declared before it it used. That's how the compiler will know that stuff exists.
You need to either move Addnumbers before main, or to do a forward declaration:
#include <iostream>
using namespace std;
int Addnumbers(int fnum, int snum);
int main()
{

C++ error: explicit qualification in declaration

I am trying to work on a tutorial that I wasn't able to finish during class and I'm having a hard time figuring out my errors. I have never seen an explicit qualification error before so I'm not even sure where to start. The only resources I can find online for this kind of error has to do when using namespaces and I don't think I am, at least not explicitly (other than namespace std).
I am sure I'm making a stupid mistake somewhere but these are the errors I'm getting:
View.cpp:12:55: error: explicit qualification in declaration of ‘void promptForAnimals(Animal**, int&)’
View.cpp:53:25: error: explicit qualification in declaration of ‘void printDance(Animal*)’
and this is my promptForAnimals function:
void::promptForAnimals(Animal* barn[], int& numAnimals)
{
//Animal* barn[MAX_ANIMALS];
int num;
string name;
cout << "How many birds? ";
cin >> num; cin.ignore();
for (int i=0; i<num; i++) {
cout << "Name " << i+1 << ": ";
getline(cin, name);
barn[numAnimals++] = new Bird(name);
}
etc
}
}
and my printDanceAnimal is empty, just has:
void::printDance(Animal*)
{
}
The errors could very well have to do with my header file, so here it is for good measure:
#ifndef VIEW_H
#define VIEW_H
#include "Animal.h"
#include "defs.h"
#include <iostream>
using namespace std;
class View
{
public:
View();
~View();
void promptForAnimals(Animal**, int&);
void printDance(Animal*);
};
#endif
You miss class name in these function definitions:
Update:
void::promptForAnimals(Animal* barn[], int& numAnimals)
void::printDance(Animal*)
To:
void View::promptForAnimals(Animal* barn[], int& numAnimals)
void View::printDance(Animal*)
void::promptForAnimals(Animal* barn[], int& numAnimals)
This is wrong. Should be:
void View::promptForAnimals(Animal* barn[], int& numAnimals)
{
// ...
}
This error appears when you explicitly specify already opened namespace.
namespace SomeName {
int SomeName::someFunc() { return 0; } //error
}
I suspect, the empty namespace is the name of the global namespace which is always open, so that is why this kind of error occurs in your case, which is equivalent to that:
int ::someFunc() { return 0; } //error again