I've been having two different problems when trying to compile a C++ project today.
Sometimes it won't reflect any of my changes in the new build: If I change some wording on the output or change around actual functionality and compile and hit Start Debugging, it will behave exactly as it did before I made the changes. Hitting Clean or Rebuild Solution fixes this, but it takes about a full minute to compile this. I guess it's not detecting any changes, but in the output window I see it list the file names of files I made changes to...
I'm also getting a lot of "...already defined in main.obj" errors (one for every function and variable) whenever I try to use a header file or define a function outside of a class. Renaming the functions lets it compile once, but then the second compile will bring up the errors again. It kinda works if I just define the class in a .cpp file, don't use a header file, and don't use any functions outside of the class.
The project is an open-source program I downloaded the other day to mess with (I'm building a bot to control it). I didn't have the first problem until today, but the second one's always been happening. All I've done is add a file (two if you count both Bot.cpp and Bot.h); Bot.cpp includes Bot.h and some files from the program, and the program's main.cpp includes Bot.cpp.
I'll post some code I guess, but I can't find anything wrong with what I'm doing. I'm wondering if there's something I need to do to the existing files? (there were VS solution files included with the project that I used to open it, since VS Express doesn't help you create projects from existing code.)
//Bot.h
#ifndef _Bot_h_
#define _Bot_h_ 1
#include <string>
class Bot{
private:
uint32 inputs = 0;
bool active = false;
public:
Bot(){};
~Bot(){};
void Start();
void Stop();
void Run();
void Wait(int seconds);
void Press(int buttons);
};
#endif
//Bot.cpp
#ifndef _Bot_cpp_
#define _Bot_cpp_ 1
#include "main.h"
//Some other project files included between these
#include "Bot.h"
using namespace std;
void Bot::Start(){
if (active == false){
active = true;
Run();
}
}
void Bot::Stop(){
active = false;
}
void Bot::Run(){
while (active == true){
printf("Has my code updated?\n");
Wait(2);
}
}
//There are more functions defined here
#endif
All I've really done in the original source code is include Bot.cpp at the bottom of the list of includes in the main.cpp file, and then add a call to create the class. I'm a little out of practice with C/C++ so maybe it's something simple. I'm also bad at writing posts like this so if anything needs clarified I'll try to help.
Related
I'm trying to use ChaiScript with SFML for my game engine. All the SFML stuff works fine, until I include chaiscript.hpp in my game object header file. My GameObject header file looks a little like this:
#include <a bunch of standard libraries>
#include "imgui.h"
struct Object {
std::string name;
void init();
void update();
void render();
*some template functions*
};
The problem occurs when I try to include chaiscript.hpp in the above file. I get a repeating error (8 of them, to be exact) from SFML's Rect.inl file: '(': illegal token on right side of '::', and then no other errors. It compiles fine when I don't include chaiscript.hpp, or when I include chaiscript.hpp in the object cpp file. I've used ChaiScript before with SDL and never had an issue like this, so is it an SFML macro messing something up? How can I avoid this problem?
It sounds like something in ChaiScript is messing up something in SFML. Reversing the order that you include them may remove those errors (include SFML before ChaiScript, or vice versa).
I'm breaking my head trying to use Servo.h inside of a library I made. The compiler consistently gives me the same error, as if it does not recognize the class, which is included in my library.
I am trying to make a new class, one of it's properties is a Servo object, which I should pass in the constructor. No matter how I try, I keep receiving the same error message when trying to compile my sketch:
In file included from /home/nezah/Arduino/My
sketches/CameraShutter/CameraShutter.ino:8:0:
/home/nezah/Arduino/libraries/Shutter/Shutter.h:13:19: error: expected
')' before '*' token
Shutter(Servo *servo);
It seems that the include statement is ok, as I get a different message if I mess it to go wrong or remove it completely. I already tried to change "" for <> and even copied the source in a folder and use the full path. No change as far as I don't mess it (on purpose). I already read this.
I also tried to pass it as a pointer, using Shutter(Servo* servo), Shutter(Servo *servo) and Shutter(Servo& servo). Same error message.
In some arduino.cc forum I read that I rather forget it and avoid using libraries inside other libraries, but I bet this is possible.
Is there anybody so kind as to give me some hints on how to do this?
I leave you part of my .h and .cpp of the library I'm trying to write (which, by the way, turns a servo into a physical button presser but with burst capability).
/*
* Shutter.h - Library to make a photocamera shutter out of a servo
* alternatively it could press any physical button with a servo.
*/
#ifndef Shutter
#define Shutter
#include "Servo.h"
class Shutter {
public:
Shutter(Servo *servo);
Servo getServo();
void shut();
private:
Servo _servo;
}
#endif
And here is my .cpp:
/*
Shutter.cpp - Library for flashing Shutter code.
Created by David A. Mellis, November 2, 2007.
Released into the public domain.
*/
#include <Arduino.h>
#include "Servo.h"
#include "Shutter.h"
Shutter::Shutter(Servo *servo) {
_servo = servo;
}
NOTE: If I remove some code and take away the "Servo" part of the constructor, I get an error message on the "getServo()" code. The problem seems to be that the compiler does not recognize "Servo" as a valid type inside my library.
Thanks in advance!
In the constructor of your class you are passing a pointer of the type Servo, so you must store that value in another pointer. To do this you must change:
*.h
#ifndef SHUTTER_H
#define SHUTTER_H
#include "Servo.h"
class Shutter {
public:
Shutter(Servo *servo);
Servo *getServo() const;
void shut();
private:
Servo *_servo;
}
#endif
*.cpp
Shutter::Shutter(Servo *servo) {
_servo = servo;
}
Servo *Shutter::getServo() const
{
return _servo;
}
Use:
Servo servo;
Shutter shuter(&servo)
Looks like the problem was that the class name and the #ifndef marker was the same, so there was a name conflict somehow. It is well explained in this thread: How to properly use a header file to be a complete class?
After fixing this, it compiled well.
Hello once again I am asking some experts for help. This time it is not about Linux commands but Visual Studio 2013 Windows Form in C++.
As always, details :
I have project with 2 forms, that's not really important.
Important things are :
I have 6 files : Windows.h, Windows.cpp, Game.h, Game.cpp, Test.h,Test.cpp
Including : Windows.cpp includes Windows.h, Windows.h includes Game.h, Game.h includes Test.h, Test.cpp includes Test.h as well.
Windows.h and Game.h are form declarations. This is where I need work to be done.
Windows. cpp is used as main, it executes whole project and do stuff.
Windows.h is Form for Menu, it just connects to Game.h Form and becomes hidden when Game appears.
Game.h is Form with game. I need to create and operate on objects in methods included in Forms connected to Buttons.
Example : Click on one button creates one object class Test with variable int number = 1 and click one other button changes this variable to 2.
I can't access same object through methods, all I can do is create two same objects and operate on them but they are different beings declared in other method. Is there any solution to construct object which can be accessed through every Form's method ?
Test.h
#pragma once
ref class Test
{
public:
int nr;
char *test;
Test();
Test(int n, char *t)
{
nr = n;
test = t;
}
};
Test.cpp
#include "Test.h"
Test::Test()
{
}
I declared objects in class Game as variables what gave me access to those objects in Game's method.
The title is pretty self explanitory: I'm trying to run a process outside of main.cpp using loadfile.cpp and loadfile.h to handle loading and displaying an image. However, Marmalade keeps throwing build errors when I do this.
At a top level, I am looking for a process that will run in main.cpp, make a call to loadfile.cpp and display an image with the code in loadfile.cpp. Ideally:
in main.cpp:
main()
{
//initialize and setup Marmalade stuff
Img* myImg; //create an image object
while (!s3eDeviceCheckQuitRequest()) {
//More Marmalade stuff
myImg->display(); //display said image
}
delete myImg;
return 0;
}
in loadfile.cpp:
#include "loadfile.h"
Img* myImg; //image object
void Img::displayImg()
//display image
and in loadfile.h:
#if !defined(_LOADFILE_H)
#define _LOADFILE_H
class File
{
public:
void displayFile();
};
extern Img* myImg;
#endif //_LOADFILE_H
Can someone point out what I am doing wrong or write a brief script showing me it? Thanks very much!
P.S. I wrote this following the Stage 2 Marmalade looking at what they do with the input.cpp/.h files and Input class. I have the full code available if that would be of help!
You need to mention all of your source files in the mkb, before you can use them in your project. Look for the mkb's source section to add the files. Once done, reload the mkb to find the newly added file in your project automatically.
Well, let's say I have simple code like that:
#ifndef SYSTEM_CLASS_H
#define SYSTEM_CLASS_H
class SystemClass {
public:
SystemClass();
bool Initialize();
}
#endif
My issue is that everything within the header guard is ommited by IntelliSense. Is it intentional? Is there any way to disable it? I tried to look for solution on SO and Google, but I found nothing. I'm using VS 2012 Express, which recently got 3rd update.