const_k const_k - 3 months ago 19
C++ Question

Mistakes occurred when using Xcode to compile C++ program

enter image description here
It's causing an Expected ';' after top level declarator, error (during build) in Xcode at this line: void intArray::writeToFile(<#char *filename#>)

I've heard that this is because Xcode add "#" to my program, is that why?And how can I remove this two "#"?

Thanks a lot.

Here's my code:

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

class intArray
{
private:
int *array;
int length;
public:
intArray(char *filename);
void sort();
~intArray();
void writeToFile(char *filename);
};


intArray::intArray(char *filename)
{
ifstream myFile(filename);
int len=300;
array=new int[len];
length=0;
while(myFile>>array[length++]);
length--;
myFile.close();
}

void intArray::sort()
{
for(int i=0;i<length;i++)
{for(int j=i;j<length;j++)
if(array[i]>array[j])
{int temp;
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}

intArray::~intArray()
{
delete [] array;
}

void intArray::writeToFile(<#char *filename#>)
{
int step=0;
ofstream outFile(filename);
for(int i=0;i<length;i+=step)
{outFile<<array[i]<<endl;
step++;
}
outFile.close();
}

void main()
{
intArray myArray("in.dat");
myArray.sort();
myArray.writeToFile("out.dat");
}

Answer

I think I've finally figured this one out, and it appears to be a side effect of how XCode handles its parameter suggestions, the ones you can tab between (they're normally blue, I think).

If you copy such a suggestion in XCode, you get the strange characters, and pasting text surrounded with those characters in XCode will turn that text blue.

If you start writing the method name, like writeToFile, XCode will suggest a parameter list taken from the declaration (it's shown in blue).
That text doesn't really "exist" until you've written it or accepted the suggestion, though, and until that text exists, the absence of it will cause confusing errors.

The fix should be pretty simple: put your cursor in the blue parameter list and press Enter to accept it (or write the text by hand).