I am getting an error at the line "void operation" when I compile, because I havent defined Gate_ptr yet. I thought of exchanging the "Gate_ptr" with just "Gate*" instead in the function def. However, is there a way to maintain my current style?
class Gate
{
public:
Gate();
void operation(Gate_ptr &gate_tail, string type, int input1, int input2=NULL);
private:
int cnt2;
int input_val1, input_val2;
int output, gate_number;
int input_source1, input_source2;
int fanout[8];
Gate* g_next;
string type;
};
typedef Gate* Gate_ptr;
Prefer this order:
//forward decleration
class Gate;
//typedef based on forward dec.
typedef Gate* Gate_ptr;
//class definition
class Gate
{
public:
//...
};
Forwared declare, do the typedef, then define the class:
class Gate;
typedef Gate* Gate_ptr;
class Gate
{
public:
Gate();
void operation(Gate_ptr &gate_tail, string type, int input1, int input2=NULL);
private:
int cnt2;
int input_val1, input_val2;
int output, gate_number;
int input_source1, input_source2;
int fanout[8];
Gate* g_next;
string type;
};
Related
**On my main i can't add a note on my new Object of the Class Trabalho
ass.add_nota(num);
**
There is a error on my compilation.
My "Trabalho.h" code:
#include <string>
#include <vector>
#include <iostream>
//#include "Enunciado.h"
//#include "Pessoa.h"
using namespace std;
class Estudante;
class Enunciado;
template <class T>
class Trabalho{
static int id_auxiliar;
string texto;
int ano;
int id;
vector<float> calif;
T* Enun;
vector<Estudante*> estudantes;
vector<Enunciado*> enunciados;
public:
Trabalho();
Trabalho(string texto, vector<Estudante*> est, T* en, int ano);
~Trabalho();
void set_texto(string texto);
string get_texto();
void add_nota(float nota);
void add_enun(Enunciado* en){Enun = en;};
int get_id(){return id;};
int get_ano() {return ano;};
void reutilizar(int id_enun);
vector<float> get_calif() {return calif;};
vector<Estudante*> get_estudantes() {return estudantes;};
Enunciado* get_enunciado() {return Enun;};
};
#endif
And my main code:
int main(int argc, char const *argv[]) {
int n;
int m;
Pesquisa ah();
float num = 1.1;
Trabalho<Pesquisa> ass();
Trabalho<Pesquisa>* tass = new Trabalho<Pesquisa>();
ass.add_nota(num);
tass->add_nota(num);
#ifndef ENUNCIADO_H_
#define ENUNCIADO_H_
#include "trabalho.h"
#include "Pessoa.h"
#include <string>
using namespace std;
class Enunciado
{
static unsigned int id_auxiliar;
const unsigned int id;
string titulo;
string descricao;
vector<int> anos_utilizados;
static unsigned int max_util;
public:
Enunciado(string titulo, string descricao);
virtual ~Enunciado();
int get_id(){return id;};
void set_titulo(string titulo);
string get_titulo();
void set_descricao(string descricao);
string get_descricao();
vector<int> get_anos_utilizados();
void mod_max_util(int a);
};
class Pesquisa: public Enunciado{
vector<string> ref;
public:
Pesquisa(string tit, string des, vector<string> refe);
};
class Analise: public Enunciado{
vector<string> repositorios;
public:
Analise(string tit, string des, vector<string> repos);
};
class Desenvolvimento: public Enunciado{
public:
Desenvolvimento(string tit, string des);
};
#endif
Both ways when i create a new Trabalho when i define my type (pesquisa is a class type on #include "Enunciado.h".
This is the two erros that appears:
"Description Resource Path Location Type
request for member 'add_nota' in 'ass', which is of non-class type 'Trabalho()' Test.cpp /Trabalho1/src line 42 C/C++ Problem
"
And:
Description Resource Path Location Type
Method 'add_nota' could not be resolved Test.cpp /Trabalho1/src line 42 Semantic Error
Can anyone help?
Thank you !
Your error is in trying to call the default constructor as
Pesquisa ah();
or
Trabalho<Pesquisa> ass();
Unfortunately, C++ is very misleading in this and it would declare your variable ass of type Trabalho<Pesquisa>(), which means "a function of zero arguments returning Trabalho<Pesquisa>" and that's exactly that the compiler error says: a function type is not a class type and as such does not have the member add_nota. Indeed, it does look exactly like a function declaration, if you look at it that way:
int main();
^ ^ ^
type arguments
name
It's a very common mistake, especially for those coming from a Java background. But it can easily catch a C++ programmer off guard as well. More information can be found here or here or here, you can see that the same error message has perplexed a good many people.
If you have a compiler conforming to the C++11 language revision, try replacing all those occurrences by
Trabalho<Pesquisa> ass{};
If not, just leave
Trabalho<Pesquisa> ass;
Unlike in Java, this does not mean that the variable will stay uninitialized. It's the C++ way to call a default (zero-argument) constructor.
I am having difficulty resolving a redefinition error. Basically, I have a class object called houseClassType in my class header file and I also have to use houseClassType as my datatype for an array within my structure in my struct header file. Below are the two header files:
house header file:
#include "Standards.h"
#ifndef house_h
#define house_h
//Definition of class, house
class houseClassType
{
//Data declaration section
private:
int capacityOfGarage;
int yearBuilt;
int listingNumber;
double price;
double taxes;
double roomCounts[3];
string location;
string style;
//Private method to set the county name
string SetCountyName(string);
string SetSchoolDistrictName(string);
//Private method to set school district name
void SetSchoolDistrictName(void);
//Set function for the object
void ExtractLocationData(string& state, string& county, string& city,
string& schoolDistrictName, string& address);
//Methods declaration
public:
///Default Constructor
houseClassType(void);
///Get methods for data members - INLINE
int GetCapacity(void) { return capacityOfGarage; };
int GetYearBuilt(void) { return yearBuilt; };
int GetListingNumber(void) { return listingNumber; };
double GetPrice(void) { return price; };
double GetTaxes(void) { return taxes; };
string GetLocation(void) { return location; };
string GetStyle(void) { return style; };
void GetRoomCounts(double[]);
//Set methods for data members
void SetCapacityOfGarage(int);
void SetYearBuilt(int);
void SetListingNumber(int);
void SetPrice(double);
void SetTaxes(double);
void SetLocation(string);
void SetStyle(string);
void SetRoomCounts(double[]);
//Output methods for data members
void OutputLocationData(ofstream&);
void OutputStyle(ofstream&);
void OutputRoomCounts(ofstream&);
void OutputCapacityOfGarage(ofstream&);
void OutputYearBuilt(ofstream&);
void OutputPrice(ofstream&);
void OutputTaxes(ofstream&);
void OutputListingNumber(ofstream&);
void OutputHouse(ofstream&);
///Destructor
~houseClassType(void);
};
#endif
Realtor header file:
#include "Standards.h"
#ifndef Realtor_h
#define Realtor_h
const int NUMBER_OF_HOMES = 30;
typedef int houseClassType;
struct realtorStructType
{
string agentName;
houseClassType homes[NUMBER_OF_HOMES]; ///Redefinition error here
int numberOfHomes;
};
void InputHomes(ifstream& fin, string agentName, int& numberOfHomes);
#endif
Any help would be much appreciated.
The C++ language likes to have unique type names throughout a translation module. The following are not unique type names:
class houseClassType
typedef int houseClassType;
If you must use the same name, then you'll need to use namespaces to separate them:
namespace City
{
class houseClassType;
}
namespace Suburban
{
typedef int houseClassType;
}
struct realtorStructType
{
Suburban::houseClassType homes[MAX_HOMES];
};
I highly recommend you draw or design this issue first. This will help you with names too.
The simple solution is to use different names.
Also, do you need the suffix "ClassType" or "StructType" in your name? In a good design, whether it be a struct or class doesn't matter.
Your code is ambiguous. If you have
class houseClassType;
typedef int houseClassType;
What would the following code mean?
houseClassType x = new houseClassType();
You can resolve the ambiguity using a namespace, but it's better to change your second houseClassType type and name.
An example might look like this.
class House {
public:
enum class Type {
...
}
};
How do I initialize the member variables in the following code?
#include <string>
#include <iostream>
using namespace std;
int main()
{
typedef struct Employee{
char firstName[56];
char lastName[56];
};
typedef struct Company {
int id;
char title[256];
char summary[2048];
int numberOfEmployees;
Employee *employees;
};
typedef struct Companies{
Company *arr;
int numberOfCompanies;
};
}
You can add a constructor like this one:
struct Employee{
char firstName[56];
char lastName[56];
Employee() //name the function as the struct name, no return value.
{
firstName[0] = '\0';
lastName[0] = '\0';
}
};
As you are already use std::string as indicated from the #include statement, you should change your classes declarations as follows (and do so outside of main() functions body):
struct Employee {
std::string firstName;
std::string lastName;
};
typedef struct Company {
int id;
std::string title;
std::string summary;
int numberOfEmployees;
Employee *employees;
};
typedef struct Companies{
Company *arr;
int numberOfCompanies;
};
first: u use typedef in a wrong way
typedef struct Employee{
char firstName[56];
char lastName[56];
};
you need the other name for typedef
typedef struct _Employee{
char firstName[56];
char lastName[56];
}Employee;
but since u're using c++ typedef is not needed.
Second: don't declare structs inside the function. Declare them outside of main.
Third: use constructors to initialize members to default value, example:
struct Employee{
Employee()
{
strcpy (firstName, ""); //empty string
strcpy (lastName, "asdasd"); // some value, every object now by default has this value
}
char firstName[56];
char lastName[56];
};
fourth: use std::string class (#include) for easier string handling
fifth: consider to type class instead struct, the only difference between those two, is that class declares the variables as private by default(you can change visibility of variable urself), while struct declares them as public by default.
I have a class declaration in my header file like shown below. One function uses one of the struct as input and the other one as return parameter. The point is when I use in this way compiler gives me error.
What would be the reason ? Any idea is appreciated.
#include <string>
using namespace std;
namespace My_Functions
{
class My_Functions
{
public:
struct {
char input_a;
int input_b;
double input_c;
double input_d;
double input_e;
double input_f;
double input_g;
} Input_Parameters;
struct {
char output_a;
int output_b;
double output_c;
double output_d;
int output_e;
} Output_Parameters;
public:
Output_Parameters FindExit(Input_Parameters input);
};
}
in cpp file
My_Functions::Output_Parameters My_Functions::FindExit(My_Functions::Input_Parameters input)
{
}
There are three ways to fix your problem.
A. struct struct_name {}; -> This declare structure called 'structure_name'
B. typedef struct {}struct_name; -> using typedef before your structure will be useful if you don't want to use 'struct' keyword before name.
C. Use struct keyword in function prototype.
struct Output_Parameters FindExit(struct Input_Parameters input);
Ex for A:
struct Input_Parameters {
char input_a;
int input_b;
double input_c;
double input_d;
double input_e;
double input_f;
double input_g;
} ;
struct Output_Parameters{
char output_a;
int output_b;
double output_c;
double output_d;
int output_e;
};
I have following code:
template<class T>
class TemplateA : public virtual std::list<T>
{
protected:
unsigned int iSize;
public:
unsigned int getSize();
};
/////////////
template<class T>
unsigned int TemplateA<T>::getSize()
{
return iSize;
}
/////////////
/////////////
/////////////
template<class T>
class TemplateB : public TemplateA<T>
{
public:
unsigned int calcSize();
};
/////////////
template<class C>
unsigned int TemplateB<C>::calcSize()
{
iSize = C.getSize;
return iSize;
}
/////////////
/////////////
/////////////
// Class C (seperate file) has to contain function getSize()
class CMyClass
{
public:
static const unsigned int getSize = 5;
};
This means, within class TemplateB I want to call the getSize method, which the passed class have defined.
I receive the following error message:
error C2275: 'C' : illegal use of this type as an expression
while compiling class template member function 'unsigned int TemplateB<C>::calcSize()'
1> with
1> [
1> C=CMyClass
1> ]
I'm quiet sure that this function worked under VS 2003... What's wrong with the method? Maybe a compiler setting? I don't know where to set what :(
You should say this->getSize or C::getSize; this will defer lookup to the second phase when the template arguments are known.
Hi you could simplify your code too whilst correcting, all you seem to have done is use C rather than TemplateB so if you do:
template<class C>
unsigned int TemplateB<C>::calcSize()
{
return c::getSize; //based on getSize being static
}
You will save the memory of an extra variable and it should work fine:)
Addendum:
Here is a working code snippet using your code as a basis:
#include <iostream>
#include <list>
using namespace std;
template<class T>
class TemplateA : public virtual std::list<T>
{
protected:
unsigned int iSize;
public:
unsigned int getSize();
};
template<class T>
unsigned int TemplateA<T>::getSize()
{
return iSize;
}
template<class T>
class TemplateB : public TemplateA<T>
{
public:
unsigned int calcSize();
};
template<class C>
unsigned int TemplateB<C>::calcSize()
{
return C::getSize;
}
// Class C (seperate file) has to contain function getSize()
class CMyClass
{
public:
static const unsigned int getSize = 5;
};
int main()
{
CMyClass classme;
TemplateB<CMyClass> test ;
cout <<"Calc size outputs: "<< test.calcSize() << endl;
return 0;
}
Apologies for the unchecked answer previously.
Hope this one helps!