function is not a member of global namespace - c++

So I'm attempting to program chess in C++ and I decided to make my move handler its own class (it was a main function before) so I could use it in some of my other classes without having to copy it everywhere.
However, when I finished setting everything up I got this error: 'parseMove' : is not a member of '`global namespace''
I'm using the standard namespace and all public functions and variables, and I know that so please don't bring it up unless it's relevant. My IDE is MS Visual C++ 2012.
Here is the code:
In the main:
void playGame(){
...
MoveParser parser; //declare move handler
...
//example of use
parser.parseMove(current, currentX, currentY);
...
}
MoveParser.h:
#pragma once
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
class MoveParser
{
public:
MoveParser(void);
~MoveParser(void);
void parseMove(string, int &, int &);
};
MoveParser.cpp:
#include "MoveParser.h"
MoveParser::MoveParser(void)
{
}
MoveParser::~MoveParser(void)
{
}
void::parseMove(string str, int &x, int &y){
//random junk
}
And also Visual C++ is giving me some new errors that aren't real...and I know they're not real because the code that it's giving me errors on ran fine before I added the new class and I haven't changed it.
Anyways, very frustrating, hopefully someone can point me in the right direction.

You forgot to specify the class name before the function name in its definition. Change this code
void::parseMove(string str, int &x, int &y){
//random junk
}
to
void MoveParser::parseMove(string str, int &x, int &y){
//random junk
}

Related

C++ circular dependency, undeclared identifier

I have to class that use each other but whatever I tried I couldn't achieve to make them work.
I just want them to access each other and after days of struggle, I decided to ask here.
If someone can point out what I am doing wrong and what I should do instead it would be great.
Edit: I decided to implement one of the solutions which are already on stack overflow and changed my code according to that:
I will also share errors this time so maybe we can figure out what's wrong.
I tried to copy this:
Resolve build errors due to circular dependency amongst classes
I used class name "Unit" instead of "A" and "Skill" instead of
"B"
Skill.h
#pragma once
#include <iostream>
#include <vector>
#include "Unit.h"
using namespace std;
class Skill :
public Unit
{
double _val;
Unit* unitPtr;
public:
Skill(double val):_val(val)
{
}
void SetSkill(Unit* unit)
{
unitPtr = unit;
unitPtr->Print();
}
void Print()
{
cout << "Type:B val=" << _val << endl;
}
// Unit* unitPtr;
vector <Skill *> attacks;
vector <Skill *> utilities;
vector <Skill *> movement;
};
Unit.h
#pragma once
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;
class Skill;
class Unit
{
int _val;
Skill* skillPtr;
public:
Unit(int val) :_val(val)
{
stunned = false;
curSpeed = speed + rand() % 8 + 1;
}
void SetSkill(Skill* skill)
{
skillPtr = skill;
skillPtr->Print(); // COMPILER ERROR: C2027: use of undefined type 'B'
}
void Print()
{
cout << "Type:A val=" << _val << endl;
}
int GetDodge()
{
return dodge;
}
void Setup();
string name;
int maxHP;
//... and other variables
};
Unit.cmp
#include "Skill.h"
#include "Unit.h"
void Unit::Setup()
{
heroes.push_back(new Vestal);
heroes.push_back(new Vestal);
heroes.push_back(new Crusader);
heroes.push_back(new Crusader);
monsters.push_back(new BoneSoldier);
monsters.push_back(new BoneDefender);
monsters.push_back(new BoneSoldier);
monsters.push_back(new BoneDefender);
}
Later on the code, I add some stuff to attacks, utilities, and moment and I want to access them from the Unit object, like below:
heroes[0]->skillPtr->attacks[0]
And I want to be able to access the variables in the Unit.h (like maxHP) from Skill.h
Errors:
Error C2512 'Skill': no appropriate default constructor available
Error C2512 'Unit': no appropriate default constructor available
I'm a beginner but maybe I can see a couple of issues that are likely causing you problems here.
First you've got a classic circular dependency here, Unit.h includes Skill.h which includes Unit.h which includes Skill.h and so on.
Secondly not seeing any #pragma once pre-proccessor directives. Which means when you then go on to try and include both Skill.h and Unit.h in your unit CPP file, you will try to include the .h files more than once, as they include each other...
Thirdly you're using namespace std in the global scope. You can do that, but don't do that.
If you're looking for more try this excellent video on the subject
https://www.youtube.com/watch?v=Zbw58vTotok

Why Do I get this error on vector initialisation?

I have a problem with vector declaration and initialization in a
class constructor. I have a Station.h and Station.cpp files of a class and I recall it in main :
Station.h
#ifndef STATION_H
#define STATION_H
#include <vector>
class Station
{
public:
int num_bin;
int num_staz;
vector<int> binari; //here already gives me error! Vector does not name a type
Station(int num_staz, int num_bin);
virtual ~Station();
Station(const Station& other);
protected:
private:
};
Then I want to initialize the vector in the constructor of .cpp like that:
Station.cpp
#include "Station.h"
using namespace std;
Station::Station(int num_staz, int num_bin)
{
this->num_bin = num_bin;
this->num_staz = num_staz;
this->binari(num_bin); //here I want to create a vector of num_bin size
}
and then call it in main like that:
main.cpp
#include <iostream>
#include "Station.h"
using namespace std;
int main()
{
Station staz1(2,3);
staz1.binari.push_back(300); // error! class Station has no member binari
staz1.binari.push_back(250);
staz1.binari.push_back(150);
return 0;
}
Where am I making a mistake?
this->binari(num_bin); //here I want to create a vector of num_bin size
The function you need to use is std::vector::resize().
this->binari.resize(num_bin);
It will be better to initialize the object with the appropriate size as:
Station::Station(int num_staz, int num_bin) : num_bin(num_bin),
num_staz(num_staz),
binari(num_bin)
{
}
this->binari(num_bin); This doesn't work because it is not an initialization that is why it doesn't work.
To make this work use it in in-class initialization list:
Station::Station(int num_staz, int num_bin) :
num_bin(num_bin),
num_staz(num_staz),
binari(num_bin)
{
}

Automatically generate method in c++

Note in the following code the function called employee_dictionary() has not been created yet. If I highlight the error symbol next to the line number then the options I get from the Eclipse IDE are: function 'employee_dictionary' could not be resolved and 'employee_dictionary' was not declared in this scope.
I assumed I would be presented with the option of having Eclipse create this function automatically to resolve the error. Now I am left wondering if there is something fundamentally wrong with my code or if Eclipse does not have the functionality I am looking for.
I am new to c++ and Eclipse, and I am building this Employee class because it is usually where I start when I want to learn a new language; help resolving the issue would be appreciated. I want an IDE that has this capability so before I get to deep if I need to switch IDE's I will.
#include <iostream>
#include <string>
using namespace std;
class Employee
{
private:
int id;
int salary;
public:
Employee(int new_id, int new_salary)
{
id = new_id;
salary = new_salary;
}
void setID(int newInt)
{
if (employee_dictonary(newInt) == 0)
{
id = newInt;
}
}
int getID()
{
return id;
}
void setSalary(int newInt)
{
salary = newInt;
}
int getSalary()
{
return salary;
}
};
int main()
{
std::cin.get();
}
You need to declare employee_dictionary as a function. You're trying to call a function that hasn't been defined yet. This is the same as trying to use a variable that you haven't defined
Example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::cout<<bob; // bob is not declared
}
In function 'int main()':
9:16: error: 'bob' was not declared in this scope

C++ var in class does not name a type

I know this question has been asked many times but none of the answers I found have been able to help me. I am trying to build a std::vector and fill it in with structs. I want to make these variables static and constant, so they can be passed around easily. My code right now is:
Melodies.h
#ifndef Melodies_h
#define Melodies_h
#include "Arduino.h"
#include <StandardCplusplus.h>
#include <vector>
struct Note {
int note;
int duration;
Note(int a, int b) : note(a), duration(b) {}
};
struct Melody {
std::vector<Note> notes;
void addNote(Note note) {
notes.push_back(note);
}
};
const Melody NONE;
const Melody BILL;
const Melody COIN;
// this gives an error
//COIN.addNote(Note(NOTE_C4, 5));
#endif
Melodies.cpp
#include "Melodies.h"
#include "Notes.h"
// this gives an error
//COIN.addNote(Note(NOTE_C4, 5));
I get the error(s):
error: 'COIN' does not name a type
How can I store this type of variable and set it 1 time like I want to do in the begin function? I am not using standard c++ - this is on an Arduino using the StandardCplusplus library.
Updated version can't work in this way - you can't change const class and definitely not outside of any function.
However something like this works (arduino.cc Arduino IDE 1.6.9):
libraries/Melodies/Melodies.h:
#ifndef Melodies_h
#define Melodies_h
#include "Arduino.h"
#include <StandardCplusplus.h>
#include <vector>
struct Note {
int note;
int duration;
Note(int a, int b) : note(a), duration(b) {}
};
using Melody = std::vector<Note>;
extern const Melody NONE;
extern const Melody BILL;
extern const Melody COIN;
#endif
libraries/Melodies/Melodies.cpp:
#include "Melodies.h"
#include "Notes.h"
const Melody NONE;
const Melody BILL;
const Melody COIN = {{NOTE_C4,20},{NOTE_E2,10}}; // whatever
melodies_sketch/melodies_sketch.ino
#include <Melodies.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
}
void loop() {
Serial.println(COIN.size()); // just print size of Melodies vector
delay(1000);
}
When you declare object as const, you can modify its member values only on initialization. If you could use your addNote() anywhere in the program then the COIN object wouldn't be constant, would it?
Error you get seems confusing, but try to make COIN non-const, or add a constructor, that will allow you to populate notes vector at initialization.

Returning array of character error in c++?

I have these two files table.cpp and table.h in my program code apart from the main.cpp. The files are described as below
table.cpp
#include <iostream>
#include "table.h"
using namespace std;
// accessor function for Name
char* PeriodicTable::Name()
{
return Name;
}
// accessor function for Symbol
char* PeriodicTable::Symbol()
{
return Symbol;
}
table.h
#ifndef TABLE_H
#define TABLE_H
class PeriodicTable
{
char Name[15], Symbol[3], GroupName[20], Block, State[25], Colour[15], Classification[20];
int GroupNo, AtomicNo, PeriodNo;
float Weight;
public:
char* Name();
char* Symbol();
};
#endif
but the problem is that the IntelliSense(since I am using Visual C++ Express 2010) shows a red curved underline below the name and symbol in the accessor function in table.cpp. I can't understand why???
Your member functions and member variables have the same name. This is not possible in C++. That's why various conventions exist for naming member variables, e.g. m_name, name_ etc. (NB: When dealing with underscores in identifiers make sure you don't use a reserved name by accident.)
You might wonder why and how that could possibly go wrong. In your example there clearly is no way to invoke operator() on char[15], but the problem is that the compiler only knows that after performing semantic analysis. There could also be cases where it is impossible to disambiguate. For example:
struct Func {
void operator()() { };
};
struct C {
Func f;
void f() {}
};
int main() {
C c;
c.f(); // which one?
}