I am trying to put some code into a separate source (.cpp) file. I created the new file, and placed in it a small routine that works when it is in Main.cpp. It starts with a while command. But that immediately gets an error “Expected unqualified-id”. I tried adding #include “vex.h” at the start of the file, but no change. Any thoughts on what is wrong here?
Code, all erer messages and screanshots would be helpful
Here is all the code in the added file: the while command receives the error.
while (Controller1.ButtonB.pressing()){;
Drivetrain.turn(right);
wait(2300, msec);
Drivetrain.turn(left);
wait(2300, msec);
Drivetrain.stop();
wait (20, msec);
}
All of the code would be needed to determine the problem.
that is all the code in the additional file. Should there be some declarations or references to header files in the added file for this added file to work? I tried all the “header” files that main.cpp uses:
#include “vex.h”
using namespace vex;
vexcodeInit();
adding them one-by-one and all together, but they made no difference
Instead of that try putting
#include “vex.h” in the additional file
Instead of “vex.h” in main.cpp file try using “addedfile.cpp”
Is there a way you could export you code to a zip file and upload it in case this doesn’t work?
I’m pretty sure you can only include header files (.h) not source files (.cpp). What I have in my program is header files that declare the functions, and the functions are then defined in the source files. I can send you an example if you want
Edit: Turns out you can technically include a source file, but if you try to define any functions or variables in that file you will get a “multiple definitions of ____” error because of the way the compiler works, and that’s why you need header files. However, that’s not the error you were getting, and I suspect your error was because of the semicolon at the end of this line:
while (Controller1.ButtonB.pressing()){;
Try deleting that semicolon, and if you then get a “multiple definitions of ____” error that would be because you need to use header files. Hope I helped!
I am thinking i need to use the approach Catzilla mentions in the second reply. But if you are interested in solving via # include, which i think others would find useful, start a completely new project, so main.cpp only has the standard code, nothing added. and the added file has only a simple while (1){ } command. see below. Taking out the #include “vex.h” blows up the main file, even if replaced by # include “added_file.cpp”: in the lines below, “while” gets the clang
include “vex.h”
while (1) {
}
Thanks. So far, I think you are right, and i used something similar to your approach to work around the issue. So i can look at your approach, can you provide the line you use to declare the function in the header file, and then the first line in the source file you use to define the function? Also, do you put #include “vex.h” at the start of the header and/or source file? i found this is necessary in my approach, but i did everything in only a source file.
Catzilla is also right.
I have also tried fixing what you had by making my own project and the problem is that you can’t have a while(1) loop by itself. It needs to have a function surrounding it.
In the added file:
#include "vex.h"
void funct(){
while(1){
}
}
In the main.cpp file everything is the same except “vex.h” has been replaced with “added_file.cpp”
Here’s an example of how I use multiple files. In this example, the files contain a function named myFunction, and a class named MyClass that has a constructor and a function in it so that you can see how to use both functions and classes in a different file. This is the header file, called MyFile.h:
// This is a header guard, which you don't necessarily need but it can be helpful
// because it makes sure that the compiler includes the file only once, so if you end
// up including it multiple times it doesn't give an error. At the end of the file is "#endif", and
// everything in between is guarded. In the lines below, where I wrote "MY_FILE" it doesn't matter
// what you write there, as long as it's different in each header file.
#ifndef MY_FILE
#define MY_FILE
#include "vex.h"
void myFunction();
class MyClass
{
public:
MyClass();
void anotherFunction();
}
#endif
This is the source file, MyFile.cpp:
#include "MyFile.h"
void myFunction()
{
// Do something
}
void MyClass::MyClass()
{
// Do something
}
void MyClass::anotherFunction()
{
// Do something
}
So to use the function or class in a different file, you just have to write #include MyFile.h
, and then you have access to them. I thought header files and header guards were very confusing at first, so I would recommend looking them up and reading a few articles about how they work.
thanks. super helpful!
Good to know, thanks