Constructor will run but not function from other file - c++

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

Related

Trying to understand the keyword friend correctly via OOP in c++, coding issues

OK so here's my question, I'm trying to understand the use of friend in C++ with a working example on my PC for reference. I have everything set up in different classes, which are connected to one another with the .h files etc. (I think anyways). Can someone show me where I'm going wrong please? Cause i keep getting compiler errors and I don't understand what it means.
Yes I've researched the C:xxxx errors online, but I can't link the problem to the code I've got... It's obviously wrong! just need a push in the right direction / better understanding of C++... thank you!
//First class
#include <iostream>
#include <string>
using namespace std;
class Tutorial48
{
private:
int var;
int secret = 5;
public:
Tutorial48(int v);
friend class Tutorial48UseFriend;
void PrintVar();
};
// First class .cpp
#include "Tutorial48.h"
Tutorial48::Tutorial48(int v)
{
var = v;
}
void Tutorial48::PrintVar()
{
cout << var << endl;
cout << "Scret variable = " << secret << endl;
}
// Second class, whole point is to demo the friend keyword in C++!
#include "Tutorial48.h"
#include <iostream>
#include <string>
using namespace std;
class Tutorial48UseFriend
{
public:
Tutorial48UseFriend();
void showSecret(Tutorial48 tut48F)
{
// Just trying to increment by 1 so i know it's worked correctly.
tut48F.secret++;
cout << "My new private variabe = " << tut48F.secret << endl;
};
};
// Main function for outputting the whole thing.
#include "Tutorial48.h"
#include "Tutorial48UseFriend.h"
#include <string>
#include <iostream>
int main
{
Tutorial48UseFriend fr;
Tutorial48 t(24);
fr.showSecret(t);
getchar();
return 0;
}
Errors being produced....
Errors
Yeah so that's everything... As i said i'm new to this, so trying to understand it. Thank you to anyone for help in advance, cheers guys.
P.s. I do kind of understand the concepts of friend in c++ how it's used to access private variables of other classes etc. but i have no idea how to code it properly...
You should define the constructor of Tutorial48UseFriend like:
Tutorial48UseFriend(){}
and also pass Tutorial48 object by reference (by using '&'), and instead of making the function void make it return an int and then print it later, so:
int showSecret(Tutorial48& tut48F)
{
return ++tut48F.secret;
}

Simple Object-Oriented Cat has not been declared

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(){}

Passing a Vector made of a Class to a function (C++)

I'm running into a lot of problems with this, primarily being that my passed Vector of my Class keeps getting flagged as an undeclared identifier. Any help on solving the problem or explanations to help me figure out what I don't understand would be greatly appreciated.
So here is a simplified version of what I have now:
main.cpp
#include "functions.h"
#include "vehicle.h"
int main()
{
int menuSelection;
vector<Vehicle> inventory;
do
{
cout << "Please make a selection:" << endl << endl;
cout << "1: Display Inventory" << endl;
.......
cout << "8 : Write inventory to file and exit" << endl << endl;
if (menuSelection == 1)
{
if (inventory.empty())
cout << "Database empty" << endl;
else
display(inventory);
.......
} while (menuSelection != 8);
return 0;
}
vehicle.h
#pragma once
#include "functions.h"
class Vehicle
{
private:
string VIN;
int year;
.......
// public:
// string getVIN();
.......
}
functions.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void display(vector<Vehicle>);
.......
void display(vector<Vehicle>& in)
{
Vehicle v;
cout << "VIN: " << v.getVIN(in)
}
.......
I've tried a bunch of different things to get it to work so that's why a lot of stuff may seem like odd syntax (I'm also not very good). My assignment is to have a menu in main.cpp which will create a vector from a class stored in vehicle.h, and then the menu is supposed to call functions which are located in functions.h that will communicate through vehicle.h to a fourth not included vehicle.cpp to work with information from the class.
In functions.h, void display(vector<Vehicle>); does not compile because Vehicle is undeclared at this point.
Also, in functions.h, void display(vector<Vehicle>& in) is a different overload to the previous prototype (the & makes a difference), probably not what you intended. And then you place a function body in functions.h -- this should not be there.
You need to organize your code so that Vehicle class definition appears, and then functions.h includes that.
So vehicle.h should look like:
#pragma once
#include <string>
// do NOT include functions.h
class Vehicle
{
// ...
};
and then functions.h should look like:
#pragma once
#include "vehicle.h"
// do NOT do "using namespace std;" in a header and don't include any unnecessary headers
void display(vector<Vehicle> &in);
and then functions.cpp should #include "functions.h" and contain the function body for display.
undeclared identifier means the compiler sees a name but don't see a declaration of that name (as a variable, class, function, etc.). It happens in your code in a few places, for example:
MyClass is used but not declared (main.cpp)
Vehicle is used but not declared (functions.h)
You use Vehicle.v but v not declared in class Vehicle (in addition, I doubt that this is what you intended - if you use class name Vehicle.v it means accessing a static variable, as opposed to vehicle.v).
It seems you lack some basic background. The most important thing is that you learn to decipher compiler errors, so at least you understand what went wrong.

How do I assign text to the "void PrintIntro" function?

So far I have typed this,
#include iostream
using namespace std;
void PrintIntro();
I want to assign a actual text now to the PrintIntro function so that in my main program I can just type
PrintIntro() and when the program runs the text assign to the function will show.
So far I have tried this after "void PrintIntro();"
{ /*PrintIntro*/
cout <<
"==================================================" << endl;
cout <<
"Welcome to the Math Practice Program!!!!!" << endl;
cout <<
"This Program will help you practice elementary math" << endl;
cout <<
"==================================================" << endl;
/*PrintIntro*/
}
But then I get the error under the "{" symbol indicating that it is "expecting a declaration." I have been searching through notes and messing with this all day and I cannot figure it out. Any help would be appreciated. I am using MS Visual studio Express 2013.
Adding the semicolon after void PrintIntro() tells the compiler that there is a function called PrintIntro that takes no arguments and returns void, and that you are defining it later. This is called a forward declaration. Chances are this is what's happening:
void PrintIntro();
//Compiler: okay, that was a forward declaration
{
//Compiler: wth is this stuff?
}
You want this to happen:
void PrintIntro();
//Compiler: okay, that was a forward declaration
void PrintIntro()
{
//Compiler: oh, this is the definition for that function you told me about earlier
}
Or you want to do it without the forward declaration:
void PrintIntro() //no ';'
{
//Compiler: declaration and function body all in one part - simple!
}
You should also change #include iostream to #include <iostream>
Remove the ; after you declare you function like:
void PrintIntro()
When declaring a function, you do not need to end the function declaration line with a semicolon.
So your function should look like
void PrintIntro(){
blahblahblah...
}
You need to remove the semi column at the end of the declaration
void PrintIntro();
Must be like this
void PrintIntro(){
}
A couple of errors, most of them in the syntax I think. Note that the line #include iostream should actually be #include <iostream> and the ; is missing after void PrintIntro().
Like so
#include <iostream>
using namespace std;
void PrintIntro(){
cout << "Hello world" << endl;
}

How to link C++ source files with Code::Blocks

I'm doing something wrong, I know. I can't quite figure out how to
link two .cpp files together through a header file. The calling
method can't see the other source.
I'm using Code::Blocks as an IDE with MinGW.
Any help would be greatly appreciated. It would be even more
appreciated if you could show the fixed source, link in the reply to a
pastebin page with it.
/***********************************main.cpp***********************************/
#include <iostream>
using namespace std;
#include "test.h"
int main()
{
printTest(); //can't see printTest, defined in test.cpp
return 0;
};
/***********************************test.h***********************************/
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
void printTest();
#endif // TEST_H_INCLUDED
/***********************************test.cpp***********************************/
#include "test.h"
void printTest()
{
cout << "Hello world!" << endl;
};
You might find this code blocks wiki helpful. It looks like Code blocks uses a managed build system so if you add the file to the project properly then it should know to compile it and link in the object file that results.
And just to be more explicit about some other comments, when you use "using namespace std;" the namespace is only brought into scope for the file where the using statement is located. That is why others are telling you to explicitly specify the std:: namespace. You could also bring all of the std namespace into scope in the test.cpp file. Many people consider this a bad habit to get into. It's generally better to bring into scope just what you need via
using std::cout;
using std::endl;
Finally, remember that std::endl adds a new line AND flushes the buffer, it's not a good replacement for a new line character in all cases.
In test.cpp replace cout << "Hello world!" << endl;
by std::cout << "Hello world!" << std::endl;
sanket answer’s seems incomplete to me.
You need to add #include <iostream> in your test.cpp so that the compiler knows what "cout" is.
As sanket stated, you should use std::cout and std::endl in test.cpp.