Azevinho
Azevinho

Reputation: 23

C++ Request for member, which is of non-class type (when using class template, can't define in the main)

**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 !

Upvotes: 0

Views: 9102

Answers (1)

The Vee
The Vee

Reputation: 11550

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.

Upvotes: 4

Related Questions