Bizarre Errors In Near Empty User Library....

I'm trying to create a new User library object, but I'm running into a really bizarre problem. I have stripped the code down to the bare bones, and the errors are still there. Here is all the code:

FlashCopy.h:

#ifndef FLASHCOPY_H
#define FLASHCOPY_H

class FlashCopy
{
public:
FlashCopy();
int copy(void);

private:
};

#endif

FlashCopy.cpp:

#include <FlashCopy.h>

FlashCopy::FlashCopy()
{
}

int FlashCopy::copy(void) {
return 0;
}

FlashCopyTest.ino:

#include <FlashCopy.h>

FlashCopy flashcopy();

void setup()
{
}

void loop()
{
}

FlashCopy.h and FlashCopy.cpp are in Arduino/libraries/FlashCopy, along with FlashCopy.txt. FlashCopyTest.ino is in Arduino/FlashCopyTest. When I compile, I get:

Compiling 'FlashCopyTest' for 'Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328'
FlashCopy.cpp : stray '\357' in program
FlashCopy.cpp : stray '\273' in program
FlashCopy.cpp : stray '\277' in program
FlashCopy.cpp : stray '#' in program
FlashCopy.cpp : expected constructor, destructor, or type conversion before '<' token
FlashCopy.cpp : 'FlashCopy' has not been declared
Error compiling

I'm especially baffled by the "stray 'xxx' in program errors. Those are not even printable characters, and there are no such characters in any of the files. I have seen this before, always when trying to create new libraries. In the past, I've just fiddled with things until they went away, without ever really knowing what was happening. This time, I've been unable to make it work, even when stripped to the skeleton library above.

Regards,
Ray L.

what editor do you use?

Please use code tags ( # button) when posting code, makes it more readable.

FlashCopy flashcopy();

Regardless of the other errors, this is wrong. The parentheses should be omitted if there are no arguments to the constructor.

I'm especially baffled by the "stray 'xxx' in program errors.

Typically, this happens when code is copied from a web page and pasted into a sketch. Some characters used in web pages, like minus signs, are not the same ASCII value as the minus sign in a sketch.

FlashCopy::FlashCopy()
{
}


int FlashCopy::copy(void) {
     return 0;
}

Consistent placement of { is good. This is not consistent.

When I copy your code into the IDE, creating three tabs, and changing the < and > to ", I get just one "error" message:

Binary sketch size: 514 bytes (of a 32,256 byte maximum)

The is typically the message we want to see.

robtillaart:
what editor do you use?

Please use code tags ( # button) when posting code, makes it more readable.

I'm using Atmel Studio but the editor is not a problem. The exact same code compiles and runs fine when combined into a stand-alone .ino file. It's only when trying to split parts out to a library that I get this problem.

Regards,
Ray L.

PaulS:

FlashCopy flashcopy();

Regardless of the other errors, this is wrong. The parentheses should be omitted if there are no arguments to the constructor.

I'm especially baffled by the "stray 'xxx' in program errors.

Typically, this happens when code is copied from a web page and pasted into a sketch. Some characters used in web pages, like minus signs, are not the same ASCII value as the minus sign in a sketch.

FlashCopy::FlashCopy()

{
}

int FlashCopy::copy(void) {
     return 0;
}



Consistent placement of { is good. This is not consistent.

When I copy your code into the IDE, creating three tabs, and changing the < and > to ", I get just one "error" message:


> Binary sketch size: 514 bytes (of a 32,256 byte maximum)


The is typically the message we want to see.

I removed the parens on the initializer, and it made no difference. The code did, long ago, originate from a web page, but has been modified ot within an inch of its life. The exact same code compiles and runs perfectly if combined into a single stand-alone .ino, so there are no "hidden" characters. I've seen this same thing happen with code I coded from scratch by hand. This only seems to occur when I try to create new library objects, and I get the same errors with VisualStudio, AtmelStudio and the Arduino IDE.

Regards,
Ray L.

so there are no "hidden" characters.

Compiling 'FlashCopyTest' for 'Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328'
FlashCopy.cpp : stray '\357' in program
FlashCopy.cpp : stray '\273' in program
FlashCopy.cpp : stray '\277' in program
FlashCopy.cpp : stray '#' in program
FlashCopy.cpp : expected constructor, destructor, or type conversion before '<' token
FlashCopy.cpp : 'FlashCopy' has not been declared
Error compiling

That's not what the compiler says. You wouldn't know if they were there or not, they're hidden.

This only seems to occur when I try to create new library objects

There is nothing fundamentally different between code in the libraries folder and code in the same folder, as I did it. Since the code compiles for me, the problem is not in the code.

PaulS:

This only seems to occur when I try to create new library objects

There is nothing fundamentally different between code in the libraries folder and code in the same folder, as I did it. Since the code compiles for me, the problem is not in the code.

That would be my conclusion as well. But WTH is it?? I'm stumped!

Regards,
Ray L.

PaulS:

This only seems to occur when I try to create new library objects

There is nothing fundamentally different between code in the libraries folder and code in the same folder, as I did it. Since the code compiles for me, the problem is not in the code.

I'm curious - what tools and OS are you running? I'm running the latest AtmelStudio, and VisualMicro, under Win8 64-bit. Since I get the same errors with the Arduio IDE, it would seem likely the problem is some Win8 and/or 64-bit compatibility issue, or something about my specific machine configuration, but darned if I have a clue how to figure out what....

Regards,
Ray L.

I think it would be a MUCH easier problem to solve if you would just listen to the compiler. It has told you exactly what its problem is and it doesn't just make stuff up. Cut and paste your code over to Word or wordpad or something that can see those characters it says it sees and just make sure they aren't there.

Or retype the code into a fresh file with no cutting and pasting and see if you don't get a nicer result.

This is like staring at your gas gauge sitting on E and saying, "I don't think I'm out of gas, now why won't my car start?"

I think it would be a MUCH easier problem to solve if you would just listen to the compiler. It has told you exactly what its problem is and it doesn't just make stuff up. Cut and paste your code over to Word or wordpad or something that can see those characters it says it sees and just make sure they aren't there.

I pasted the code OP posted into the IDE. The characters are not there.

It is not at all like staring at the gas gauge on E, and wondering why you can't start the car.

OP: I'm using 1.0.5 on Win7 64 bit.

So..... I opened each file in Notepad, then re-saved it. No difference. I deleted all the files, copy and pasted from my first post in this thread. No difference. Those characters are NOT in any these three files! I suspect they're being found in some include file, library file or something else that's happening behind the scenes. That would also explain why I've seen this problem before, with code that was freshly typed in. But what file is it coming from? How do I find it when the compiler gives me absolutely no useful information about exactly where it's seeing them?

Regards,
Ray L.

Tim Leek at VisualMicro figured it out. No idea why, but the .h file (which was created by doing a File->New in AtmelStudio), was saved in UTF-8 format. The fix was to open the file in Notepad, then do a SaveAs, and change the Encoding from UTF-8 to ANSI. It now compiles correctly. Must be a bug in Atmel Studio to create a file with that encoding when the tools apparently only work right with ANSI encoding....

Thanks for your help, Paul!

Regards,
Ray L.

I think GCC defaults to UTF-8 so it may not be that simple, but if you have got some spurious multibyte characters in there then forcing the file to a single byte format would be one way to eliminate them. Naturally, copying and pasting the text via the clipboard gives multiple opportunities for the character encoding to be modified which would prevent us from seeing the problem. If you had attached your original files, we would probably have been able to reproduce the problem and tell you exactly what was wrong and where.

FlashCopy.cpp : stray '\357' in program
FlashCopy.cpp : stray '\273' in program
FlashCopy.cpp : stray '\277' in program

Octal 357 is hex EF
Octal 273 is hex BB
Octal 277 is hex BF

UTF-8 Files and the Preamble

The UTF-8 preamble, also known as the UTF-8 BOM or signature, is a 3 byte sequence at the start of a file indicating it is UTF-8. Like the UTF-16 BOM, this is not particular to XML, it is for any text file. But unlike the UTF-16 BOM, Byte Order Mark is not a correct term in this case because in UTF-8 there is no byte order. In hex, the UTF-8 preamble is ef bb bf.

By saving the file as UTF-8 those 3 bytes (EF BB BF) were written as the first three bytes of the file. That is why the compiler complained about those specific numbers.

Good catch - UTF-8 without BOM works fine, but adding the BOM causes exactly the error seen.

Well it is nice to have a logical explanation. The only thing that has be baffled is how the file became UTF-8 in the first place. An empty .h file was created by using File->New in Atmel Studio, then I cut and pasted text from a working sketch into it. Until today, I had no idea Notepad handled anything but plain old-fashioned ASCII. Live and learn.

In any case, my new library is now working perfectly - I can now "clone" the firmware from one Arduino directly onto another, using nothing but a serial cable between the two Arduinos.

Regards,
Ray L.