简体   繁体   中英

Am I creating an object correctly in the header file?

So I'm trying to use classes to do a linked list. I've never used classes on c++. What I'm trying to do is simply to create an object of the class palabra on the header file.

I want to access primeraPalabra on the .cpp file for later methods. I am not able to create the object.

The error that I'm getting is: multiple definition of "primeraPalabra".

I'm also new with the use of headers and cpp files. any tip is appreciated.

palabra.h file

#ifndef PALABRA_H_INCLUDED
#define PALABRA_H_INCLUDED

#include <iostream>
using namespace std;


class Palabra{
    private:
    int p_id;
    string p_nombre;
    int p_valor;

    Palabra *next;
    Palabra *prev;


public:
    Palabra(int id, string nombre);
    void printNombre();
    int calcularValorPalabra();

    void agregarSiguiente(Palabra *);
    void eliminar();
    void buscar();
};

Palabra primeraPalabra(0,"");
Palabra ultimaPalabra(0, "");
Palabra palabraTemp(0, "");


#endif // PALABRA_H_INCLUDED

palabra.cpp file

Palabra::Palabra(int id, string nombre)
    {
    p_id = id;
    p_nombre = nombre;
    p_valor = calcularValorPalabra();

    next = NULL;
    prev = NULL;
}
Palabra primeraPalabra(0,"");
Palabra ultimaPalabra(0, "");
Palabra palabraTemp(0, "");

This, in your header file, defines and instantiates three objects. Every .cpp that includes this header file will define these three objects. You obviously have multiple .cpp files; so that's your multiple definition error that your compiler and linker eventually barks at you about.

An #include is logically equivalent to inserting the contents of the header file, verbatim , into the .cpp file. Remove these definitions from the header file, and manually add them to every .cpp file that includes this header file, and you'll get the same error.

You need to declare these objects, using the extern keyword in your header file. Then, define these objects in exactly one of your .cpp files, in whichever one you feel is more appropriate to define them.

Your C++ book should have plenty of examples of doing this kind of a thing, and how to use the extern keyword.

PS That using namespace std; , in your header file no less, is going to give you a world of hurt sooner or later. You need to get rid of it. Since, you explain, you're just starting to learn C++, this is the best time for you to avoid picking up bad habits. Simply forget that "using namespace std;" exists in C++, for now, until you fully understand what it does. It may seem irritating, at first, but it won't be long before you won't even be consciously aware of your fingers automatically typing std:: before every template, class, and function from the C++ library.

If you would like to create an instance of the object, this should be done wherever it is needed. For now lets assume this will be done in a main.

Created a new file called: main.cpp

Inside have the following:

#include "Palabra.h"
int main() 
{
 Palabra palabra_one(0, ""); 
 return 0; 
}
Palabra primeraPalabra(0,"");
Palabra ultimaPalabra(0, "");
Palabra palabraTemp(0, "");

A critical issue with the given header file can be seen in the following lines. you are creating 3 instances of a class inside a header file

First of all, we should ask ourselves: "What is a header file?" suppose you have many many function declarations and your program has many different classes and structs definitions. Well, one option is to put them all inside a .cpp file. Well, that would be very messy and the code wouldn't be that modular. The question we should ask ourselves is: "How can we seperate our function & methods definitions & declarations and classes & structs definitions into different & unique files?"

Unsurprisingly, header files can help us.

Suppose you have 2 classes A & B, and you wish to separate their declarations and definitions into different files. Here's the conventional way to do it: Ah - A file that will contain declarations related to A only A.cpp - A file that will contain implementations of the functions & methods declared in Ah Bh - A file that will contain declarations related to A only** B.cpp - A file that will contain implementations of the functions & methods declared in Bh

One might ask: "How would you use the functions and methods declared in header files?" In order to include a header file in your source code you can type the following above your C/C++ code

#include "filename.h"

************** Extra Note ************** Before someone screams at me in the comments - Yes, template functions implementations should to be in the header file and not in a cpp file If you aren't aware of what templates are - https://en.wikipedia.org/wiki/Template_(C%2B%2B) (Extra material) But that's a whole different topic ************** Extra Note **************

How would that compile????????? Well, between compilation and the final executable there's a middle stage called linking . a linker is responsible for combining all of the used header files specified; You can read more about the linking process here: https://en.wikipedia.org/wiki/Linker_(computing)

Conclusion - the problem in the code

You essentially wrote lines for creating 3 instances of a class in a header file . These lines are completely out of place and were probably written there by accident, however, due to the fact that you specified you are new to the way header works this is a general introduction with a few buzz-words you can feel free to google. If you have any question, ask me in the comments!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM