"... was not declared in this scope"

When I build it, it returns an error in the ".cpp".
The error is that neither "titulo" or "nodo->autor" are declared in their scopes.
My question is, in the private, must I put something like "struct nodo{ string titulo; string autor; etc..} or maybe the problem is in the constructor?
Thanks.

////////////////////////////////////////////////////////////////////////////////
#ifndef BOOK_H
#define BOOK_H
#include <iostream>
#include <string>

using namespace std;

class book{
private:
int titulo;
string autor;
string editorial;
int paginas;
string genero;
string contenido;
string ruta;

public:
book();
~book();
void CargaTitulo(string t);
void CargaAutor(string a);
void CargaEditorial(string e);
void CargaPaginas(int p);
void CargaGenero(string g);
void CargaContenido(string c);
void CargaRuta(string r);
string DevuelveTitulo();
string DevuelveAutor();
string DevuelveEditorial();
int DevuelvePaginas();
string DevuelveGenero();
string DevuelveContenido();
string DevuelveRuta();
};
#endif // BOOK_H

////////////////////////////////////////////////////////////////////////////////
#include "book.h"
// Estructura de Libros
#include <iostream>
#include <string>
using namespace std;

book::book()
{

}

book::~book()
{
}

void CargaTitulo(int t)
{
titulo = t;
}

void CargaAutor(string a)
{
nodo->autor = a;
}

////////////////////////////////////////////////////////////////////////////////
Last edited on
needs to be:

1
2
3
4
5
6
7
8
9
book::void CargaTitulo(int t)
 {
 titulo = t;
 }

book::void CargaAutor(string a)
 {
 nodo->autor = a;
 }
If I do that, it returns the next error:

expected unqualified-id before 'void'
check the syntex. http://www.cplusplus.com/doc/tutorial/classes/
1
2
3
4
5
6
7
8
9
void book::CargaTitulo(int t)
 {
 titulo = t;
 }

void book::CargaAutor(string a)
 {
 nodo->autor = a;
 }
Just a nit, but generally it's good practice to pass strings by reference; you're currently passing them by value.

Use void book::CargaAutor(const string& a);

To indicate that A) You're accepting a reference ("&"), which means you are directly accessing the string object that the function was called with (which is very efficient for heavy weight types like string), and B) You won't be making any changes to that object ("const"), as all you're doing is assigning a field to it.

Also, if you're not doing anything in the constructor or destructor, you should just not declare them. The compiler will generate default implementations for you.
Last edited on
I thank you in advance, wcassella, for replying.

I just tried that thing that you have said, but I am sorry to say that it does not work.

I am running out of ideas


The problem is that in the .cpp file you are not prefixing the method names with the name of the class ("book"). As sujitnag pointed out, it needs to be qualified as
void book::CargaTitulo(int t) { }
In order to define it.

Without that the compiler thinks you are just defining a free-floating function called "CargaTitulo", so referencing class members doesn't work, hence the error.

One thing to note though, there actually isn't anything preventing you from just defining the methods inline in the header file, and dropping the .cpp file altogether. Just define the methods where you declare them in the class, and it will be fine.
Last edited on
jesus i wasn't even drunk when i typed my reply :/
void before the class name and scope resolution operator.
Sorry about that.
Topic archived. No new replies allowed.