I was doing experiments with my Arduino today and this was one of them. I have commented the code well so it should be easy to understand. Here you go:
I was developing some C++ stuff on my Linux virtual machine and wanted to create something that would make it easy for me to compile C++ code. So I wrote up some bash code and added it into .bashrc as a function. Here’s the code:
echo "Checking if output directory exists..."
if [ ! -d "./output" ]; then
echo "Creating output directory..."
echo "Output directory created."
elif [ -f "./output/cpprun.o" ]; then
echo "Backing up previous output..."
mv ./output/cpprun.o ./output/cpprun.bak.o
echo "Backup finished."
echo "Running g++ compile..."
g++ -o ./output/cpprun.o *.cpp
if [ -f "./output/cpprun.o" ]; then
echo "Compile finished."
echo "Running output..."
echo "Run finished."
echo "Compile failed."
I have just started learning C++ and have been getting a lot of these errors. I finally found a solution to this after spending some time on the internet.
Say for instance you have a class called “Animal” and g++ complains that this class is being redefined. Just add the following two lines at the very top of the Animal.h file:
and add the following at the very bottom of the file:
So, if your class is called SomeClass, then it will be SOMECLASS_H instead of ANIMAL_H. You should have these in every header file. It prevents that header file from being redefined more than once.
This is incredibly easy in C#. First of all, you need to have a string from which you want to extract words. A word is a string separated by spaces. To do that, just say:
string s = "Welcome to my blog!";
Now, when I use the
Split command, it will return an array of individual strings. So, the implementation will be:
string words = s.Split(" ",StringSplitOptions.RemoveEmptyEntries);
As a result,
words array will contain “Welcome”, “to”, “my”, “blog!”.
Threading in C# is no big deal. It has the easiest implementation I’ve ever seen. First of all, you need to have a method that you want to execute on a different thread. For this instance, I’ll put method called Calculate(). Now, this method has to be a void. However, it can take any amount of parameters. So, it can be:
public void Calculate()
public void Calculate(int x, int y)
Now that we know what to execute, we need to create a thread with this method. This is done by:
Thread t = new Thread(Calculate());
I can now run the thread by typing:
See Also: How do I do threaded programming in Java?