I recently read somewhere that std::cout
is an instance of the std::ostream
class. I wish to implement a similar kind of thing. I make a class Animal
and want to provide an instance of Animal
class dog
in the library itself like std::cout
. I am not sure how to do it but here's a code snippet which hopefully will give you an idea of what I'm trying to achieve.
// lib.h
#ifndef LIB_H
#define LIB_H
#include <string>
class Animal {
public:
Animal();
std::string name;
};
Animal dog;
dog.name = "dog";
extern Animal dog;
#endif
// lib.cpp
#include "lib.h"
Animal::Animal() {}
Animal dog;
dog.name = "dog";
// main.cpp
#include "lib.h"
#include <iostream>
int main() {
Animal my_dog = dog;
std::cout << my_dog.name << std::endl;
return 0;
}
This is the error I get when I try this code:
lib.cpp:6:1: error: ‘dog’ does not name a type
dog.name = "dog";
This kind of code may seem silly, but I still am looking for ways to implement this approach and not its alternatives.
Aucun commentaire:
Enregistrer un commentaire