I am really new to C++, and I am trying to create a class in a separate file, and I ran into a problem. I basically copied the tutorial http://thenewboston.org/watch.php?cat=16&number=15 from the newboston word for word. However, for the things don't work. I am getting this error when I am trying to run the main file:
C:\Users\Akavall\Desktop\C++ Stuff\New C++ stuff\class_try.o:class_try.cpp|| undefined reference to `Burrito::Burrito()'|
||=== Build finished: 1 errors, 0 warnings ===|
Also, when I am creating the class. The Workspace icon is sitting by itself, while it is supposed to (I believe) include my .cpp and .h folders of the just created class.
My guess is that my paths are not set correctly somewhere, but I have no idea how to fix that. Any suggestions?
Here is the code that I am using:
Main file (class_try.cpp)
#include <iostream>
#include "Burrito.h"
using namespace std;
int main()
{
Burrito bo;
return 0;
}
The class files:
Burrito.h
#ifndef BURRITO_H
#define BURRITO_H
class Burrito
{
public:
Burrito();
};
#endif // BURRITO_H
Burrito.cpp
#include "Burrito.h"
#include <iostream>
using namespace std;
Burrito::Burrito()
{
cout<<"something silly"<<endl;
}
What should I do to fix the this problem?
Thank You in Advance
Edit:
I am using CodeBlocks, and I am on 32bit windows.
If you are using MSVC like that tutorial you linked, you need to first create a project. After creating the project, add all the header (.h) and source (.cpp/.cxx) files that you want to include/compile in it. Now, MSVC makes this easy from this point; you can simply compile (as long as everything is properly added to your project) and it will compile things in order for you.
Extra Information
The information below is not directly pertinent to this issue (or at least doesn't seem that way since you're on Windows), but is intended to help your growth as a developer if you want it.
If, however, you ever decide to use a command line compiler (i.e. MingW) or work in a *nix environment, it is important to note that you need to explicitly compile each object (without linking; in gcc/mingw this flag is -c). Then at the end of compiling several unlinked object, you compile all the objects and the main source file together (with linking this time) and it will create one executable.
I would recommend, however, before trying a command line compiler that you get comfortable/familiar with doing this process through an IDE. Since it is generally an automated process anyway (i.e. most developers end up writing Makefile's for this kind of thing, realistically) you do not really lose too much (immediately, anyway) by not doing this. It is just something to be aware of as you learn.
Using command line compiler you can try this:
g++ class_try.cpp Burrito.cpp -o executable_name
This will link your two .cpp files and create an executable file and you can run that executable file just by typing
./executable_name
Related
I've been using code blocks for a long time, but never really made my programs into actual code blocks projects. I tried to do it today, and I kept getting errors due to code blocks not recognizing my files. Here is what I have : ---->
CodeBlocks Include Error
When I try to buiild my project I get that cout,cin and my class objects are not defined in my menu.cpp file. So I can only guess code blocks is not properly handling the files.
I would love if someone could help me out as to why this is happening.
Thanks a ton in advance :)
When I try to buiild my project I get that cout,cin and my class objects are not defined in my menu.cpp file.
That's because they're not. You #included neither iostream nor class.h in menu.cpp, so you can't access the declarations therein.
Note that Code Blocks (just like any properly set up build tools) will compile each cpp file separately. This means that not only will it compile menu.cpp as part of the compilation of main.cpp (because you include it), it will also compile it on its own. In the latter case the includes from main.cpp will not be available, so menu.cpp needs its own includes.
This also means that once it does compile (i.e. once you added the includes), you'll get a linker error because the definitions from menu.cpp are now defined twice (once in main.o -- because you included menu.cpp in main.cpp -- and once in menu.o). That's the reason why you should never include cpp files into each other.
PS: This is unrelated to your problem, but it's considered bad practice to use using namespace in a header file. You should put that in your cpp files instead (if you want to use it at all). You should also put the #include <iostream> in those files where you actually need it, rather than the header file.
My program is extremely simple and I'm just learning how to work with prototypes by making header files.
My folder setup is like this:
other.cpp
other.h
entry.cpp
My file, entry.cpp, looks like this:
#include "other.h"
int main(){
doSomething();
return 0;
}
doSomething is a function declared in other.cpp and is prototyped in other.h like so:
//ifndefine pre-processor statements
void doSomething();
Building is where I run into trouble. Runningg++ main_c_file.cpp -o name.exe in the console is supposed to create the executable. The problem I am having is that it doesn't know what doSomething is.
I suspect it's because the processor is not searching for other.cpp. I assumed, based on the tutorial I am following, that I didn't actually need to specify this file; it worked fairly well in their video to simply include just the header file and have the cpp file in the same directory.
further: I downloaded their source code and the problem remained. It is not my code, it is how I am trying to build my files. I am not using an IDE to build my programs (I am using Atom IDE to write programs if you know of a shortcut through there), and like stated earlier I am just running the Mingw g++ cppfile.cpp -o command.
Besides the straight answer, if there is a way that can make building cpp projects easier please link me to it or write it here. I have had nothing but trouble in the past trying to learn this language.
I think I can do it in eclipse.
BTW, every time I make a new C++ file with the system default template.
It has #include <stdio.h> instead of #include <iostream>
How to fix it?
Why can't I put many separate source files with MAIN function in one project?
Because your linker can only allow a single implementation of a function per linking object, but you try to give him multiple int main implementations.
Because you did not configure XCode to behave differently, it tries to link all files into one binary object, leading to this conflict.
I think I can do it in eclipse.
Quite surely not, unless you forgot to add the source files to the list of files that end up in your binary, or you forget to define a binary that links everything together.
First of all, I am a beginner programmer so, please give simple answers that are understandable.
I am suing Code::Blocks and I am trying to make a multiple file project in C++. I am using SDL2. My problem is that when I put all my codes and functions altogether, they run and when I seperate them, they don't. This is because the execution file requires object files to be built which it don't get. It don't get them because compiler don't form them. In other words, there are problems with linking. The compiler simply, said that there are not ".o" files. These files are in the project as "Link Files" and not "Compile Files". And when I make them "Compile Files" as well as "Link Files", they produces errors that various variables inside the file where I mentioned them, are not declared. But when I include that file in the file using the variables, it gets deeply nested.
Following are the two ways:
First way which is not working, with seperate files.
In main.cpp:
#include <SDL.h>
#include <other.h>
int var;
int linkVar;
int link1Var;
int link2Var;
#include "link.cpp"
#include "link1.cpp"
#include "link2.cpp"
int main( int argc, char* args[] )
{
linkFunc();
link1Func();
link2Func();
}
In link.cpp/ link1.cpp/ link2.cpp:
void linkFunc()/void link1Func()/void link2Func() //Just a reference
{
//Code associated with var, link1Var, link2Var and link3Var.
}
Second way which is working, with all functions in a single file.
First thing is that I don't want to learn makefile thing. If there is any other way to solve it then be it!
Code::Blocks makes object files for each file that it compiles as separate compilation units. It will compile and link together any files that are part of the project which it identifies as a source file.
First, remove your #includes of the cpp files. Then try the Project > Add files... menu option to add those other cpp files to the project.
First of all, you should not use #include on other cpp files, you can read about it here:
include cpp
Secondly when you are trying to compile a project composed of separate files you should use the g++ command like this: g++ main.cpp link.cpp link2.cpp etc'
you can read a basic explanation about it here: use the g++ command
To do exactly what you asked you should do:
# get rid of including any cpp files
# use forward declerations on any function belonging in other cpp files, before your main function like this:
void linkfunc();
void linkfunc2();
etc
.
.
.
int main()
{
}
# specify the direct path to your .h files like this:
#include <C:/MinGW/SDL2/SDL.h>
# use this command:
g++ -I C:/MinGW/SDL2/ main.cpp link.cpp link2.cpp link3.cpp
The problem I am having is that, when I call a constructor for a class I have created I get the following error.
main.cpp:20: undefined reference to
`StaticObject::StaticObject(Graphics*, sf::String,
sf::Vector2)'
This problem can be 'fixed' adding an include for the .cpp file in main.cpp like so.
...
#include "GameObjects/StaticObject.cpp"
...
Although this solves the problem, this seems like a poor solution and goes against what I have been previously told. Is there any other way to solve this problem? I'm using Netbeans 7.3 with g++ to code/compile this program. Below is the relevant code.
main.cpp
...
#include <SFML/Graphics.hpp>
#include "Graphics/Graphics.hpp"
#include "GameObjects/StaticObject.hpp"
int main(int argc, char** argv) {
//SETUP
Graphics graphics;
background = new StaticObject(&graphics, "Data/Images/BackgroundPlaceholder.png", sf::Vector2f(0,0));
...
main.hpp
...
#include <SFML/Graphics.hpp>
#include "GameObjects/StaticObject.hpp"
...
// Objects
StaticObject *background;
...
StaticObject.hpp
#include <SFML/Graphics.hpp>
#include "../Graphics/Graphics.hpp"
class StaticObject{
public:
StaticObject();
StaticObject(Graphics *_graphics, sf::String texture_filename, sf::Vector2f _position);
StaticObject(const StaticObject& orig);
virtual ~StaticObject();
private:
// The sprite stores the position
sf::Sprite *sprite;
sf::Texture *texture;
};
StaticObject.cpp
#include "StaticObject.hpp"
#include <SFML/Graphics.hpp>
#include "../Graphics/Graphics.hpp"
StaticObject::StaticObject(){
}
StaticObject::StaticObject(Graphics *_graphics, sf::String texture_filename, sf::Vector2f _position) {
sprite = _graphics->AddSprite(texture_filename);
sprite->setPosition(_position);
}
StaticObject::StaticObject(const StaticObject& orig) {
}
StaticObject::~StaticObject() {
}
If I add the following line to main.cpp, the error disappears.
#include "GameObject/StaticObject.cpp"
Can anyone please explain:
Why this fixes the problem?
Why the .cpp was not implicitly included through including the .hpp
file?
Is there a better way of doing this?
The undefined reference error indicates that the definition of a function/method (i.e constructor here) was not found by the linker.
StaticObject::StaticObject(Graphics*, sf::String, sf::Vector2<float>)
And the reason that adding the following line:
#include "GameObject/StaticObject.cpp"
fixes the issue, is it brings in the implementation as part of the main.cpp whereas your actual implementation is in StaticObject.cpp. This is an incorrect way to fix this problem.
I haven't used Netbeans much, but there should be an option to add all the .cpp files into a single project, so that Netbeans takes care of linking all the .o files into a single executable.
If StaticObject.cpp is built into a library of its own (I highly doubt that is the case here), then you might have to specify the path to the location of this library, so that the linker can find the implementation.
This is what ideally happens when you build your program:
Compile: StaticObject.cpp -> StaticObject.o
Compile: main.cpp -> main.o
Link: StaticObject.o, main.o -> main_program
Although there are ways in gcc/g++ to skip all the intermediate .o file generations and directly generate the main_program, if you specify all the source files (and any libraries) in the same command line.
The linker cannot find the definition for StaticObject, which means you have not compiled and linked StaticObject.cpp. Each .cpp file needs to be separately compiled, and the object files the compiler produces for each one needs to be given to the linker.
The reason including StaticObject.cpp in Main.cpp works is that you are telling the preprocessor to insert the contents of StaticObject.cpp into Main.cpp, which you are compiling, so the definitions become part of Main.cpp's compiled output.
including the cpp file causes the compiler to build that code as part of that file (which you should not do), what you need to do is compile the GameObject/StaticObject.cpp file as its own object, and link the 2 objects together.
I dont know much of netbeans, but probably GameObject/StaticObject.cpp is not included as part of the sources to compile.
That's why if you include it manually at main.cpp he found source code and everything is ok.
I had the same issue but mine was because I'm using Eclipse CDT with and Autotools project. So I had to manually add the new .h and .cpp files to the corresponding Makefile.am, and then do a project > reconfigure project, rebuild, an that was it.
I had the same issue in Qt, where I forgot to add the class to the pro file. Apparently, without adding it to the pro file, the compiler doesn't recognize it. So I added it in, and then ran qmake again, and that silved the problem.
Basically, you need to add the file in the sources.