Here is my header file. Im trying to compile this but i got an error at void init(TipMatrice tip) "initializing argument 1 of 'void graf_matrice::init(graf_matrice::TipMatrice)' " which redirects me to the main with another conversion error. I think its something wrong with my enum.
#ifndef GRAF_MATRICE_H
#define GRAF_MATRICE_H
class graf_matrice
{
public:
static const int MaxN = 50; // numarul maxim de noduri
static const int Infinit = 99999; // nu exista drum intre noduri
enum TipMatrice
{
MatriceAdiacenta = 1,
MatriceCosturi = 2
};
public:
graf_matrice();
void init(TipMatrice tip); //iniţializare graf
};
#endif // GRAF_MATRICE_H
Here is my main. I got an error at g.init(1) "invalid conversion from int to graf_matrice::TipMatrice" .
#include <iostream>
#include"graf_matrice.h"
using namespace std;
int main()
{
graf_matrice g;
g.init(1);
return 0;
}
Could you please help me with this cuz i dont know what is wrong. Sorry for bad language im beginner.
As you've defined an enum, you'd better use its values properly:
g.init(graf_matrice::MatriceAdiacenta);
Explanation: the enumerators (here MatriceAdiacenta) in an unscoped enum (here TipMatrice) are defined in the scope containing the enum (here the class graf_matrice). This is why you have to indicate the scope with ::
Alternatively, if you really need to convert from an int, you could still do it explicitly:
g.init(static_cast<graf_matrice::TipMatrice>(1));
Related
There's the main.cpp that has lots of Log prinkled wherever:
#include "utils.hpp"
...//some code
int main(){
int a = 0;
int b = 0;
util::LogClass::Log("Initial","something);
//some more code
util::LogClass::Log("Mid","something");
//some more code
util::LogClass::Log("Middle","something");
}
And the LogClass is defined like this in utils.hpp:
namespace util{
class LogClass{
public:static bool LOG_ENABLED;
public: static void Log(std::string tag, std::string message){
if(LOG_ENABLED){
std::cerr << tag+": "+message <<std::endl;}
}
}
bool LogClass::LOG_ENABLED=true;
}
I was thinking I would be able to do this in main.cpp:
#include "utils.cpp"
util::LogClass::LOG_ENABLE=false;
int main(){ //the previous main function}
*the above code actuallly gives an error saying: redefinition of ‘bool util::LogClass::LOG_ENABLED’
bool util::LogClass::LOG_ENABLE=false *
but, if I move it inside the main:
#include "utils.cpp"
int main(){ util::LogClass::LOG_ENABLED=false; //the previous main function}
then the code compiles fine. So my question is why can't I enable it outside the main() function even if it is a static member, and why does the (g++) compiler takes it as a redefinition?
You can only statically initialize a variable at the point where it is getting defined. The initialization inside the main function is dynamic, so that's fine.
I agree that the compiler error is weird though - the compiler might be trying to auto-deduct the "missing" type that should be there for a redefinition.
I am pursuing some interest in c++ programming by way of self instruction. I am working on some basic stuff for now and am currently having issue getting my classes talking/instantiated?.
I am trying to get my main cpp file to compile alongside a header and call to some class functions through the main using a more efficient command method.
I am stuck and would appreciate some help. I will include both files. I am just trying to get a return value from the header by calling the function.
error:
main.cpp:6.21 error: cannot call member function 'void myClass::setNumber(int) without object
the code works when compiled with the main, so it is something with the 'scope resolution operator' i think. First is main.cpp
#include <iostream>
#include "myClass.h"
using namespace std;
int main(){
myClass::setNumber(6);
{
return number;
}
}
Then my header file myClass.h
// MyClass.h
#ifndef MYCLASS_H
#define MYCLASS_H
class myClass {
private:
int number;//declares the int 'number'
float numberFloat;//declares the float 'numberFloat
public:
void setNumber(int x) {
number = x;//wraps the argument "x" as "number"
}
void setNumberFloat(float x) {
numberFloat = x;
}
int getNumber() {//defines the function within the class.
number += 500;
return number;
}
float getNumberFloat() {//defines the function
numberFloat *= 1.07;
return numberFloat;
}
};
#endif
Any help?
The error message says everything:
cannot call member function 'void myClass::setNumber(int)' without object
You need to create an object first:
myClass obj;
then call the class method on that object:
obj.setNumber(6);
The value 6 will get assigned to the number field of the obj variable.
I've got 2 classes, casilla.cpp and casilla.h.
In the cpp one I get the error of class redefined, and in .h "there's a previous definition of the classs casilla. I've searched for it in the internet, but not even putting casilla:: before one or putting the headers work. Here's the code:
Casilla.h:
#ifndef CASILLA_H_
#define CASILLA_H_
using namespace std;
class Casilla { //previous definition of ‘class Casilla’
public:
casilla(); //ISO C++ forbids declaration of ‘casilla’ with no type [-fpermissive]
virtual ~casilla(); //expected class-name before ‘(’ token
void SetNumeroCasilla (int _numero);
};
/* namespace std */
#endif /* CASILLA_H_ */
Casilla.cpp:
#include "Casilla.h"
#include "Tablero.h"
using namespace std;
#include <iostream>
#include <iomanip>
class Casilla //errors :Multiple markers at this line
- redefinition of ‘class Casilla’
- Line breakpoint: Casilla.cpp [line:
17]
{
int fila;
int columna;
int numero;
public:
// default constructor
Casilla::Casilla()
: fila(-1)
, columna(-1)
, numero(0)
{ }
int GetNumero() {return numero;}
void SetCasillaPosition (int _fila, int _columna) //set a cell position
{
fila = _fila;
columna = _columna;
}
void SetNumeroCasilla (int _numero) //set a cell value
{
numero = _numero;
}
void SetCasillaFull (int _fila, int _columna, int _numero) //set a cell position and value
{
fila = _fila;
columna = _columna;
numero = _numero;
}
};
Just changed the code with new errors shown. The redefined error persists, what did I do wrong?
In casilla.cpp, you're redefining casilla... class casilla { .. }; is a class definition, and you have it twice: once in your header and once in your cpp. Hence, the redefinition error.
All you need to do in the .cpp is provide definitions for the class methods you declared in your .h:
#include "Casilla.h"
// other includes
// define the default constructor:
casilla::casilla()
: fila(-1)
, columna(-1)
, numero(0)
{ }
// define this function
void casilla::SetNumeroCasilla (int _numero)
{
// something
}
I am creating a data structure but when I try and compile I get an error saying that I haven't specified that type of set that I am initializing.
I am working with the NTL library with is used for large numbers.
This is my code:
#include <set>
#include ...
NTL_CLIENT
using namespace std;
using namespace NTL;
const RR ZERO = to_RR(0);
const RR ONE = to_RR(1);
const RR TWO = to_RR(2);
class tenTree
{
public:
tenTree(string newName = "", int newLevel = 0);
~tenTree();
void put(string prefix, RR power);
bool get(string prefix, RR & output);
void display(int depth);
bool isKnown(RR power){return (powers.find(power) != powers.end());};
private:
tenTree* children [10];
set<int> powers;
int level;
string name;
bool child[10];
};
When I try to compile it comes back with an error saying:
twoPow.cpp:47: error: ISO C++ forbids declaration of \u2018set\u2019 with no type
twoPow.cpp:47: error: expected \u2018;\u2019 before \u2018<\u2019 token
twoPow.cpp: In member function \u2018bool tenTree::isKnown(NTL::RR)\u2019:
twoPow.cpp:44: error: \u2018powers\u2019 was not declared in this scope
Is there something that I am missing here?
It was just a matter of the scope. All I had to do was add an std:: before the set and it compiled correctly.
I am trying to implement the a map from the C++ STL as follows:
#include <string>
#include <iostream>
#include <map>
using namespace std;
#include "assembler.h"
// This Class makes use of the Map Template from the Standart Template Library
// All addresses are stored as numerical (Dec) integers
SymbolTable::SymbolTable() { // Constructor
map <string, int> symbolTable;
int address = 0;
}
void SymbolTable::addEntry(string symbol, int address) {
symbolTable[symbol] = address;
address++;
}
// Returns true if symbolTable already contains symbol
bool SymbolTable::contains(string symbol) {
if (symbolTable.find(symbol) == symbolTable.end()) { return true; }
else { return false; }
}
int SymbolTable::getAddress(string symbol) {
return symbolTable[symbol];
}
I try to compile this with
c++ *.cpp -0 assembler.out
and I get the following error message:
symboltable.cpp:57:9: error: no viable conversion from 'mapped_type' (aka 'std::basic_string<char>') to 'int'
return symbolTable[symbol];
^~~~~~~~~~~~~~~~~~~
1 error generated.
I have searched for this error online and all I get is bug reports relating to the STL and I cannot figure out if those reports are the same problem I am having and if so how to get around it. Am I doing something wrong?
I have tried (probably stupidly) to typecast the offending line as
return (int) symbolTable[symbol];
Thank you for any help.
My header file declares the class as:
class SymbolTable {
public:
SymbolTable();
void addEntry(string, int);
bool contains(string);
int getAddress(string);
private:
map <string, string> symbolTable;
int address;
};
This:
SymbolTable::SymbolTable() { // Constructor
map <string, int> symbolTable;
^
^
is a function-local variable, not a member variable. It is not the same as the symbolTable that you're accessing in e.g. getAddress, which presumably is a member variable. You haven't shown the class body, but my guess is that it's defined differently.