Simple Object-Oriented Cat has not been declared - c++

Here is a video i made with visuals of my issue
https://www.youtube.com/watch?v=KqvQivVfAdI
I am following an online tutorial to learn C++. Everything has been going great so far. I am using code::blocks and the person teaching is using Eclipse.
I have this specific code he told me to write, and it isn't working with code::blocks. I'm getting an error in Cat.cpp:
'Cat' has not been declared
Is there something I have to do differently since I am in code::blocks?
His video, if interested...
Here is my code:
main.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
int main(){
Cat cat1;
cat1.speak();
cat1.jump();
return 0;
}
Cat.h
#ifndef CAT_H_INCLUDED
#define CAT_H_INCLUDED
class Cat{
public:
void speak();
void jump();
};
#endif // CAT_H_INCLUDED'
Cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
/*Error on this line*/
void Cat::speak(){
cout << "Meouwwww1!!! " << endl;
}
/*probably on this on too */
void Cat::jump(){
cout << "Jumping to top of bookcase1" << endl;
}
#include <iostream>
using namespace std;
code put together
#ifndef CAT_H
#define CAT_H
class Cat
{
public:
Cat();
void speak();
private:
};
#endif // CAT_H
void Cat::speak()
{
cout << "Meouwww2!!!" << endl;
}
int main(){
Cat cat1;
cat1.speak(); /*error happening here now
error: id returned 1 exit status*/
return 0;
}
picture of errors
https://gyazo.com/bb3574ea504f2357c7ea2987facc9874

error: id returned 1 exit status
No. The actual error is
undefined reference to Cat::Cat()
error: ld returned 1 exit status
There is no any "id" which can return any exit status. There is ld, which is a linker. Its job is to create the executable file, after your files are compiled. It takes all your compiled files, and links them together. If there is a function which you call in your files, but it is not defined in your files, the linker searches for it in standard libraries, or in any additional libraries which you provided, and adds the function (and functions it calls) to your executable.
Here the problem is, you implicitly call for constructor for your class Cat, and you declare it, but you hadn't provide it. Obviously, the linker cannot find constructor for your class in standard libraries.
There are two ways to deal with it. One is to provide the constructor, i.e.
Cat::Cat() {
}
Alternatively, you may remove the line
Cat();
from your code. In this case, the default constructor will be generated authomatically. (For each class, the constructor without parameters is authomatically generated, unless any constructor is provided.)

You need to add the constructor and destructor for your class Cat.
In the header for a quick fix you could do:
Cat(){}
~Cat(){}

Related

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;
}

Unable to call member functions

I'm pretty sure I'm probably doing something stupid, but I've been at this an hour and a half and can't figure out what I'm missing.
I can create an object from my class using the default constructor, but can't use an overloaded constructor when I add one. I can't call the print member function that I have included or any others that I have tried to include either. I have put the three files into a Code::Blocks project and gotten the same result. I have also tried the three files on Dev-Cpp with the same result. Any help would be greatly appreciated.
Main Function
#include <iostream>
#include "Appt.h"
using namespace std;
int main()
{
Appt a();
a.print();
}
Appt.h
#ifndef APPT_H
#define APPT_H
#include <iostream>
#include <string>
using namespace std;
class Appt
{
public:
Appt();
void print();
private:
string description;
};
#endif // APPT_H
Appt.cpp
#include "Appt.h"
using namespace std;
Appt::Appt()
{
description = "No Description";
}
void Appt::print()
{
cout << description << endl;
}
I am using Code::Blocks 16.01 with the GCC compiler. These files are not currently in a project. I am also running Windows 7.
It looks like your problems may be related to this line:
Appt a();
Unfortunately, while this looks like it calls the default constructor, it actually declares a to be of type Appt(), that is, a function taking no arguments and returning Appt. If you want to call the default constructor, there are a few options:
Appt a;
Appt a = Appt();
Appt a{}; // requires C++11
I would prefer the last one.

Why are my functions undefined when I declared the type already?

Hi I was just trying to learn separate Classes in C++. I don't know why my code is not working.
So here is the main file code
#include <iostream>
#include "Number.h"
using namespace std;
int main()
{
Number key;
key.setNumber(200);
cout<<key.getNumber();
return 0;
}
Here is the Class cpp functions file code
#include "Number.h"
#include <iostream>
using namespace std;
void Number::setNumber(int transfernumber)
{
privatenumber = transfernumber;
}
int Number::getNumber()
{
return privatenumber;
}
And here is the header file
#ifndef NUMBER_H
#define NUMBER_H
class Number
{
public:
Number();
void setNumber(int transfernumber);
int getNumber();
private:
int privatenumber;
};
#endif // NUMBER_H
Thanks
In your cpp file you need to define the default constructor for the Number class. For example:
Number::Number() : privatenumber(0) {}
I have test your example. The error happened for the main.cpp cannot found the number.cpp. You have three ways to solve it:
write your main() to the number.cpp, not a solo file.
complie the main.cpp with the linux command gcc or write a Makefile, instead of using codeblocks.
If you want to use the codeblocks for compiling, you should create a project, and then add your three files to the project. Now compile the main.cpp.
Use the three ways above, I think you will compile successfully.
BTW, you should add the Number::Number() 's implementation.

C++ Namespace ofstream Won't Write

I'm working on making a game in C++. I have declared a Constant namespace used for global values that I need access to throughout the program. In there I have an ofstream for debugging purposes (yeah, I know it's not "constant" but it fits best there), which outputs only when it feels like it. I was able to make a small program demonstrating the problem. I apologize for it being spread across 4 files, but it is important, I promise.
main.cpp:
// Include necessary files
#include "test.h"
#include "constants.h"
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
// Start of program
Constant::outstream.open("test.txt");
// ...
// Do stuff
// Output debugging info
Test test;
test.print("Test", Constant::outstream);
// ...
// Do other stuff
// End of program
Constant::outstream.close();
return 0;
}
constants.h:
#ifndef _CONSTANTS_H
#define _CONSTANTS_H
#include <fstream>
namespace Constant
{
static ofstream outstream;
}
#endif
test.h:
#ifndef _TEST_H
#define _TEST_H
#include <string>
#include <fstream>
#include "constants.h"
class Test
{
public:
void print(string str, ofstream& out);
};
#endif
test.cpp:
#include "test.h"
using namespace std;
void Test::print(string str, ofstream& out)
{
out << "out: " << str << endl << flush; // Works
Constant::outstream << "Constant::outstream: " << str << endl << flush; // Doesn't
}
In the test.cpp file, the out << ... line works as it should, while the Constant::outsream << ... line doesn't do anything even though I'm passing Constant::outstream as the out parameter! I don't see any reason why these two lines should be in any way different.
Before posting this, I tried putting test.cpp's code in test.h, just to have less files for the question, and was amazed to see it work. If I copy-paste the Test::print() function into test.h (whether inside or out of the class Test { ... }), then both output commands work correctly. the problem only occurs if Test::print()'s implementation is in a separate file.
It seems like any references to Constant::outstream simply don't work in class cpp files (no compile error, just nothing happens). It works in main.cpp and in class header files, but any class cpp file it seems not to. Unfortunately, this is a big program I'm writing so pretty much every class has its own cpp implementation file, and that's really the one place I need to use this ofstream. Does anyone know the reason for this?
Thanks in advance,
Doug
Constant::outstream has internal linkage, thus a separate instance is created for each translation unit. In short, Constant::outstream in test.cpp and main.cpp are two different variables.
§3.5.2 A name having namespace scope (3.3.6) has internal linkage if it is the name of
— a variable, function or function template that is explicitly declared static; or,
On the other hand, static class members would be visible throughout the program.
So, if you would write
struct Constant
{
static ofstream outstream;
}
instead of
namespace Constant
{
static ofstream outstream;
}
it would work.
However, note that the class must have external linkage; e.g. you should not put in in anonymous namespace.

Problem separating .h and .cpp file

I am writing a class and need to separate the declarations from the implementation, but I keep receiving "undefined reference" errors when compiling and linking my test program. It works fine when I include the implementation in the .h file, so I believe I am doing something wrong in there. I just can't figure out what.
Huge_Integer.h
#ifndef HUGE_INTEGER_H
#define HUGE_INTEGER_H
#include <vector>
#include <string>
using namespace std;
class Huge_Integer
{
public:
Huge_Integer();
Huge_Integer(string);
void input();
string output();
void add(Huge_Integer);
void subtract(Huge_Integer);
bool is_equal_to(Huge_Integer);
bool is_not_equal_to(Huge_Integer);
bool is_greater_than(Huge_Integer);
bool is_less_than(Huge_Integer);
bool is_greater_than_or_equal_to(Huge_Integer);
bool is_less_than_or_equal_to(Huge_Integer);
private:
vector<int> value;
};
#endif
Huge_Integer.cpp
#include<vector>
#include<string>
#include<iostream>
#include "Huge_Integer.h"
using namespace std;
// all stubs for now...
Huge_Integer::Huge_Integer()
{
cout << "object created\n";
}
Huge_Integer::Huge_Integer(string s)
{
cout << "object created\n";
}
//etc...
It also works if I put #include "Huge_Integer.cpp" in my test file, but I shouldn't have to do that, right?
I am using MinGW.
Thanks in advance!
Edit: Added stubs from my .cpp file
Sounds like a linking issue.
What that means is that you have to compile your class first -- this will create a compiled object file.
Then compile the main program while passing in this compiled version of the class.
Like this:
g++ -c huge_integer.cpp
g++ main.cpp huge_integer.o
Substitute your mingw command for g++ if it is different.
Not related to linking, but you are referring to Huge_Integer inside the class declaration itself.
At least with g++, you should add a forward declaration before so that Huge_Integer has meaning inside the class declaration thus:
class Huge_Integer; // forward declaration
class Huge_Integer {
Huge_Integer();
// etc...
void add(Huge_Integer);
Note: I don´t have comment privileges, so I had to type in the answer box.