C++ Error: identifier 'class name' is undefined - c++

I have just started to use classes in C++. As a first object-oriented project, I want to program a character creator for a Pen and Paper game.
I have created the class structure including inheritance.
This is an example class:
#include "Characters.cpp"
#include "Fighter.cpp"
#include "Dwarf.cpp"
class FighterDwarf : public Characters, public Fighter, public Dwarf {
public:
string test = "Hello, I am a fighter dwarf!";
void testPrint() {
cout << test << endl;
}
FighterDwarf() {
}
};
And here is main:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
FighterDwarf fighterDwarf;
fighterDwarf.testPrint();
return 0;
}
In case it is not obvious, they are in two different files/Items. I don't think it is important but they are also in two different folders.
The problem is that I get the error message "Error: identifier "FighterDwarf" is undefined.
It is most likely a beginner mistake. I would guess that I must somehow declare "FighterDwarf" in the main file before I can create an instance of the class. If this is the case, I don't know how to do that with classes.
By they way, I know that there are a lot of people out there not liking multiple inheritance but I have chosen to use C++ and not Java because of that very feature.
SOLVED:
If I want to do everything in a single file, I need to do that in the header. Which I will, as some of the classes have some more code which I don't want to rewrite right now. Furthermore, I find it easier tow work with one file than two, at least at the beginning where I have more important things to mind. But I will split the classes up into two in my very next program. By the way, why is it so important to split classes up? Isn't it easier to work with one class?

It has nothing to do with classes. Your main.cpp file simply needs to #include the header file which declares and/or defines your FighterDwarf class.

Related

Undefined reference to a defined method

So I was trying to access a method that is defined in another class and has the prototype in the header. I'm pretty positive I defined it but it keeps popping up undefined reference to SafeCracker.
Main.cpp
#include <iostream>
#include "mystuff.h"
using namespace std;
void BigDog(int KibblesCount);
int main()
{
cout << SafeCracker(1);
return 0;
}
mystuff.cpp
#include <iostream>
using namespace std;
string SafeCracker(int SafeID)
{
return "123456";
}
mystuff.h
using namespace std;
#ifndef MYSTUFF_H_INCLUDED
#define MYSTUFF_H_INCLUDED
string SafeCracker(int SafeID);
#endif // MYSTUFF_H_INCLUDED
Here it tells you that you have an undefined reference, so you don't really have a problem with the prototype.
Had you forgotten to include the header file that contains the prototype you would have gotten something like
main.cpp: In function ‘int main()’:
main.cpp:8:13: error: ‘SafeCracker’ was not declared in this scope
cout << SafeCracker(1);
Your undefined reference is a linker error. The most likely cause would be that you did not use mystuff.cpp when compiling
If you're compiling from the command line, you should give both files as parameters.
If you're using an IDE that calls the compiler, make sure that the file is part of the project.
For example in Code::Blocks right-click on the file name and go "add to project" (If I remember correctly)
It is also possible that you made a typo in the function declaration in mystuff.cpp (that doesn't seem to be the case here though)
Now there is one important thing about your code you should take note of:
It is very bad practice to put a using namespace in a header file.
using namespace std; in a .cpp source file is mostly up to you, and that using statement will only apply to that particular file.
But if you put it in a header file that is meant to be included through #include , the using there will be forced upon any code that includes it.
Here is an example:
main.cpp
#include <iostream>
// including mystuff.h to use that awesome SafeCracker()
#include "mystuff.h"
// I need to use an std::map (basically an associative array)
#include <map>
// the map of my game
class map
{
int tiles[10][10];
};
int main()
{
// The std map I need to use
std::map<int, int> mymappedcontainer;
// The map of my game I need to use
map mytiles;
// The reason why I need to include mystuff.h
cout << SafeCracker(1);
return 0;
}
Normally, my class map should not be a problem since the map I included from the standard library is inside the namespace std, so to use it you would need to go std::map.
The problem here is that, since mystuff.h has using namespace std; in it, the symbol map is already used, and that creates a conflict.
You do not now who will use your header files, or if you will use them again a long time from now, and maybe then you will want to use name that is already used in the std namespace.
I advise you to use std:: before things taken from the standard libraries instead (std::string instead of just string for example)
PS: In C++, "class" refers to a class data structure, and the functions you made here are not part of any class. You should say "defined in another file" or "defined in another translation unit" instead

Constructor will run but not function from other file

Sorry new to C++ here and cannot find the answers that I am looking for anywhere. I am trying to run the simplest possible program in C++ using OOP and multiple files. If the Vehicle class has no doSomething() function in it, then the constructor prints out just fine. When I add the function and call car.doSomething() it just gives me errors. I have searched for days and can't find a working answer.
main.cpp
#include <stdio.h>
#include <iostream>
#include "Vehicle.h"
using namespace std;
int main(int argc, char **argv){
Vehicle car;
car.doSomething();
return 0;
}
Vehicle.cpp
#include "Vehicle.h"
Vehicle::Vehicle(){
cout << "do something" << endl;
}
void doSomething(){
cout << "do something else" << endl;
}
Vehicle.h
#pragma once
#include <iostream>
using namespace std;
class Vehicle{
public:
Vehicle();
void doSomething();
};
Like I said, new to C++ and not sure how to fix this. Thanks for any help.
Specs:
Codelite v10.0.0,
Linux Ubuntu 18.04
Error: undefined reference to 'Vehicle::doSomething()'
You didn't need to search for days; you only needed to read the chapter in your C++ book about defining member functions.
It goes like this:
void Vehicle::doSomething()
{
cout << "do something else" << endl;
}
That Vehicle:: is how the computer knows that this doSomething definition is for the class Vehicle (just like you did already with the constructor).
Without that, it's just an ordinary function. It doesn't matter that the file is called Vehicle.cpp; C++ doesn't really care about filenames. You could have all sorts of functions, variables, class definitions etc in that file, regardless of whether it were called Vehicle.cpp or Stuff.cpp or Lightness4Eva.cpp (that's not to say that your naming convention is ungood, though!).
I suppose you have "unresolved" linker error in this case. It means error is not in runtime but in build time. Error message could prompt you that linker can't find Vehicle::doSomething(). This would point you that you didn't actually provide doSomething() function. Read error outputs, it helps to understand what is wrong.
write doSomting() defination with class name as in .cpp file:
void Vehicle::doSomething()
{
cout << "do something else" << endl;
}

c++ expects ';' at end of member declaration when there is one

I have two errors that arise on the same line of code.
The following are snippets, only relevant code is there.
First is Database Class
#include "program.h"
using namespace std;
class Database {
public:
Database(Program *program);
~Database();
};
Next is the Query Class.
#include "database.h"
using namespace std;
class Query {
public:
Relation* evaluate(Database* database);
};
The line in question is:
Relation* evaluate(Database* database);
The first error is "Database does not name a type", despite including the database.h file.
The second error is "expected ';' after member declaration" which I believe is related to the first.
Everything in this project has worked until the addition of this database class.
I am stumped on this one, and have been researching all over stackoverflow to no avail.
I am on Debian Stretch and compiling with g++. Does anyone have any ideas?
EDIT
Minimal compilable code:
query.h
#include "database.h"
class Query {
public:
void evaluate(Database* database);
};
program.h
#include "query.h"
#include "database.h"
using namespace std;
class Program {
public:
};
database.h
#include "program.h"
class Database {
public:
Database(Program *program);
};
You might want to try out the -E option of the compiler: it causes the source code to be preprocessed, i.e., you'll see the code as it is seen by the compiler. If you look at your headers closely you'll see that there are cyclic dependencies between the different types. You'll need to break them.
The "easy" fix is to declare the Database class in query.h:
class Database;
class Query {
public:
void evaluate(Database* database);
};
While this will fix the shown problem, cyclic dependencies will come and haunt you. In general they are bad. A system of class which cyclicly depend on each other are effectively just one component and should probably be all declared in just one header.

C++ Boost.Test - Where should class object be created to test it's method?

I am doing exercism.io C++ challenges which use Boost to test the code. I have 3 files, bob.cpp, bob.h, and bob_test.cpp(all below). Without classes, I can get the tests to run fine. But once I need to test a class method, like in bob_test.cpp, which attempts to test bob::hey([arg]), then I get the error:
error: cannot call member function ‘std::__cxx11::string bob::hey()’ without object
So I clearly need to instantiate bob somewhere(Ex: bob bob; ... I didn't choose the names) but I just can't figure out where to do it. The Boost test framework provides it's own main function(which means I don't provide one), so I can't do it there, and I kind of expected that Boost would instantiate the object itself, but it doesn't seem to. I have tried inserting bob bob; into bob_test.cpp and bob.cpp resulting in the same error. My question is, where should I then instantiate a bob object that can be used in bob_test.cpp? Being a C++ noob, my gut says it should be in bob_test.cpp, but I'm also pretty confident I'm not supposed to edit that file.
bob.cpp
#include "bob.h"
#include <iostream>
#include <string>
using namespace std;
string bob::hey() {
return "Whatever.";
}
bob.h
#include <iostream>
#include <string>
// bob.h
class bob {
public:
std::string hey();
};
bob_test.cpp(just providing the first test(simplified) causing the error, the actual test is slightly different, I just want to get the setup working)
#include "bob.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(stating_something)
{
BOOST_REQUIRE_EQUAL("Whatever.", bob::hey());
}
You could use a boost::test fixture to create an instance of class bob and then test bob's functions in separate test cases, see: Fixture models. E.g.:
#include "bob.h"
#include <boost/test/unit_test.hpp>
struct TestFixture
{
bob bob_instance;
TestFixture()
: bob_instance()
{}
~TestFixture() = default;
}
BOOST_FIXTURE_TEST_SUITE(TestBob, TestFixture)
BOOST_AUTO_TEST_CASE(stating_something)
{
BOOST_REQUIRE_EQUAL("Whatever.", bob_instance.hey());
}
BOOST_AUTO_TEST_SUITE_END()
hey is a non-static member function and you're trying to use it as a static. You need to create an object. This is C++ bitching at you, not Boost.
bob mybob;
mybob.hey();
You could also just do this: bob().hey()

why does not eclips know cout and cin in my code?

I am new in c++, I want to write my program with eclips but it does not know cout and cin however I add include
This is my code:
class READY {
public:
READY();
virtual ~READY();
#include <iostream.h>
int main (){
cout<<"hello";
}
};
#endif /* READY_H_ */
Move the include and main outside the class and qualify cout with std:::
#include <iostream>
class READY {
public:
READY();
virtual ~READY();
};
int main (){
std::cout<<"hello";
}
C++ is not Java, main resides at the global scope, not as a class member.
Also, it's <iostream>, not <iostream.h>.
Whatever tutorial or book you're following... it isn't any good.
You need to put that #include at the top of your file; including headers in the middle of a class will do weird, weird things! At the very least, it will embed all the names in the header into your class; most likely, it will simply fail to compile.
Furthermore, modern C++ puts cout and essentially every other symbol defined in the standard library into a namespace named std, so you need to write std::cout, or put "using namespace std;" before your class definition, but after the #include.
There are a couple of problems with your code:
The #include statement has to be outside the class declaration. It is good practise to put those at the top of the file and not scatter them through the file as it makes it much easier to check for dependencies in your code by eyeballing the top of the file instead of searching the whole file for #includes.
Your main() function also has to be declared and defined outside the class. In contrast to Jave, main() in C and C++ is a standalone function.
As mentioned, cin and cout live in the std namespace. I would recommend referring to them with the fully qualified name (ie, std::cin and std::cout), although you can use using std::cin; and using std::cout; either inside the function or in your implementation file after all includes
You are including iostream.h - that is the "wrong" file as that is for the old iostreams library. The correct include for the standard compliant iostreams is <iostream>