C++ error: explicit qualification in declaration - c++

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

Related

error: '' was not declared in this scope

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

Struggling with C++ "was not declared in this scope"

Can anyone tell me why i get the error "name was not declared in the scope when running this?
Thanks.
class lrn11_class{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string lrn11_name;
};
int main()
{
lrn11_class lrn11_nameobject;
lrn11_nameobject.setname("Zee");
cout << lrn11_nameobject.getname() << endl;
return 0;
}
This should work - see comments (BTW use std:: - Why is "using namespace std" considered bad practice?)
#include <iostream>
#include <string>
class lrn11_class{
public:
void setName(const std::string& x){ // Potentially saves copying overhead
name = x;
}
std::string getName() const { // Look up const and its uses
return name;
}
private:
std::string name; // - Used: string lrn11_name; but functions use name!
};
int main()
{
lrn11_class lrn11_nameobject;
lrn11_nameobject.setName("Zee"); // Fixed typo
std::cout << lrn11_nameobject.getName() << std::endl; // Ditto
return 0;
}
You have declare lrn11_name as a member varible for this class. But in set and get functions you are using name.
Other than than you need to call functions as you have defined.
so instead of :-
lrn11_nameobject.setname("Zee");
cout << lrn11_nameobject.getname() << endl;
You have to use following code :-
lrn11_nameobject.setName("Zee");
cout << lrn11_nameobject.getName() << endl;
Make sure that
#include <iostream>
using namespace std;
should be included.

C++ Passing vectors to an overloaded class - some call statements work, some do not.

I have spent a great deal of time in google trying to figure out how to pass a vector when using .h and .cpp files between a call in main and a function in an includes block. I was successful using class definitions.
Now everything is going fine until I want to create an overloaded function. (I could have done this with two different classes, but I must use one overloaded function in my program.)
Here is my writeData.h file:
#ifndef WRITEDATA_H
#define WRITEDATA_H
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class writeData
{
public: writeData();
public: writeData(vector<int> & DATAFILE);
public: writeData(vector<int> & DATAFILE, string);
};
#endif
The placement of the using namespace std; is another topic.
Here is my writeData.cpp file:
#include "writeData.h"
writeData::writeData()
{
std::cout << "Default writeData" << std::endl;
}
writeData::writeData(vector<int> & DATAFILE)
{
cout << "writeData 1" << endl;
for (int var : DATAFILE)
{
cout << var <<endl;
}
}
writeData::writeData(vector<int> & DATAFILE, string fileName)
{
ofstream myfile(fileName);
cout << "writeData" << endl;
if (myfile.is_open())
{
for (int var : DATAFILE)
{
cout << var << endl;
myfile << var << endl;
}
myfile.close();
}
}
And here is my main function:
#include <iostream>
#include <vector>
#include <string>
#include "writeData.h"
using namespace std;
int main()
{
string fileName = "test.txt";
vector<int> items{ 10, 14, 22, 34 };
writeData();//default
///////////////////////////////////////////////////
// the next line is the problem code:
//writeData(items);//writes to screen only
//<<When I uncomment it the compiler Tosses the following:
// 'items': redefinition; different basic types
////////////////////////////////////////////////////
writeData(items, fileName);//writes to screen and to file
cin.ignore();
cin.get();
}
The offending line is writeData(items);
Any assistance or pointers to online articles would be most appreciated.
The immediate issue is that this declaration
writeData(items);
is the same as
writeData items;
hence the redefinition error. The deeper issue is that you have defined three constructors for a class, and seem to be attempting to call them without making a named instance. To succesfully call the one parameter constructor passing items, you'd need something like
writeData data_writer(items);
Alternatively, you may want either member functions, or non-members. The choice would depend on whether you really want to model a class, which maintains certain invariants or not. An example of members,
class writeData
{
public:
void write_data() const;
void write_data(const vector<int> & DATAFILE) const;
void write_data(const vector<int> & DATAFILE, string) const;
};
Then
WriteData wd;
wd.write_data(items);
Example of non-members:
namespace writeData
{
void write_data();
void write_data(const vector<int> & DATAFILE);
void write_data(const vector<int> & DATAFILE, string);
};
Then
writeData::write_data(items);
Note I have made the vector<int> parameters const reference because they are not being moified in the functions.

C++ header issue involving functions and scope

My problem is in the following C++ code. On the line with the 'cout' I get the error:
"'number' was not declared in this scope".
.h
using namespace std;
class a{
int number();
};
.cpp
using namespace std;
#include <iostream>
#include "header.h"
int main(){
cout << "Your number is: " << number() << endl;
return 0;
}
number(){
int x = 1;
return x;
}
Note: I'm aware this isn't the cleanest code. I just wanted to get the function working and refresh my memory on how to use headers.
For minimal fix, three basic changes are necessary.
Proper implementation of the number() method
int a::number() {
int x = 1;
return x;
}
Proper invocation of the number() method
a aObject;
cout << "Your number is: " << aObject.number() << endl;
There are many other enhancements possible though.
Addition, as pointed out by #CPlusPlus, usable scope of number() method, for example declaring it public
class a{
public:
int number();
};
Try this in your cpp file
using namespace std;
#include <iostream>
#include "header.h"
void a::number()
{
int x = 1;
return x;
}
int main()
{
cout << "Your number is: " << a().number() << endl;
return 0;
}
As for your header file replace class with a struct. The reason you are getting this error is because the compiler cant find the variable number. It is actually a method of a class.The reason you are replacing the class with a struct is because by default everything in a struct is public. So your header file called header.h should look like this
using namespace std;
struct a
{
int number();
};
There are three issues with your code.
The definition of the function number().
As you declared, it is a member function of the class "a". In your .cpp, the class name should be used as a prefix to the function. I mean,
a::number(){
int x = 1;
return x;
}
As the function is a member of the class "a", there are only two ways of accessing it,
If the function is a static function in the class, you can access it with :: operator. Like a::number().
If the function is not a static function, that is true in your case, you should instantiate the object out of the class "a" and they use "." operator with the reference. I mean,
a obj;
obj.number().
Your function number() is declared in private scope. You may recall that by default the scope is a class is private unless you specify public or protected. So the private function number() cannot be used outside the declared class unless there is a friend to it.
Below the code that I fixed,
.h
using namespace std;
class a{
public:
int number();
};
.cpp
using namespace std;
#include <iostream>
#include "header.h"
a::number(){
int x = 1;
return x;
}
int main(){
a obj;
cout << "Your number is: " << obj.number() << endl;
return 0;
}

Cout and endl errors

I have listed my code below. I get soooo many errors saying cout and endl was not declared in this scope. I do not know what I am doing wrong or how to force the class to recognise cout? I hope I am explaining my problem correctly. If I comment out the methods (not the constructor) it works. I am probably just making a novice mistake here - please help.
using namespace std;
class SignatureDemo{
public:
SignatureDemo (int val):m_Val(val){}
void demo(int n){
cout<<++m_Val<<"\tdemo(int)"<<endl;
}
void demo(int n)const{
cout<<m_Val<<"\tdemo(int) const"<<endl;
}
void demo(short s){
cout<<++m_Val<<"\tdemo(short)"<<endl;
}
void demo(float f){
cout<<++m_Val<<"\tdemo(float)"<<endl;
}
void demo(float f) const{
cout<<m_Val<<"\tdemo(float) const"<<endl;
}
void demo(double d){
cout<<++m_Val<<"\tdemo(double)"<<endl;
}
private:
int m_Val;
};
int main()
{
SignatureDemo sd(5);
return 0;
}
The compiler needs to know where to find std::cout first. You just need to include the correct header file:
#include <iostream>
I'd suggest you not to pollute the namespace using using directives. Instead either learn to prefix std classes/objects with std:: or use specific using directives:
using std::cout;
using std::endl;