I'm new in c++. my friend just gave this code to me but it doesn't work and sends many syntax errors like : error C2146,error C2734,... which I'm not familiar so I thought it should be better to ask stack overflow.
pixel.cpp:
#include<iostream>
#include"a.h"
using namespace std;
extern const unsigned uint_8 microsoftSansSerif_8ptBitmaps[];
extern const unsigned FONT_INFO microsoftSansSerif_8ptFontInfo;
extern const FONT_CHAR_INFO microsoftSansSerif_8ptDescriptors[];
int main()
{
getchar();
}
a.h :
// Font data for Microsoft Sans Serif 8pt
const unsigned uint_8 microsoftSansSerif_8ptBitmaps[] = {
0b11110000,
0b00010000,
0b00101000,
0b00101000,
0b01000100,
0b01000100,
0b01111100,
0b10000010,
0b10000010,
};
const FONT_CHAR_INFO microsoftSansSerif_8ptDescriptors[] =
{
{7, 0}, // A
};
const FONT_INFO microsoftSansSerif_8ptFontInfo =
{
2, // Character height
'A', // Start character
'A', // End character
2, // Width, in pixels, of space character
microsoftSansSerif_8ptDescriptors, // Character descriptor array
microsoftSansSerif_8ptBitmaps, // Character bitmap array
};
errors :
a.h(2) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
a.h(2) : error C2146: syntax error : missing ';' before identifier 'microsoftSansSerif_8ptBitmaps'
a.h(3) : error C2059: syntax error : 'bad suffix on number'
a.h(3) : error C2146: syntax error : missing '}' before identifier 'b11110000'
a.h(4) : error C2059: syntax error : 'constant'
Some problems:
uint_8, FONT_INFO, and FONT_CHAR_INFO are not declared anywhere.
Unnecessarily adding unsigned before custom types is an error.
Binary literals require compiling in c++11 mode, and a compiler which supports that.
To use getchar, you need to include <cstdio>.
Generally, you should put the extern declarations in the .h file and the definitions in the .cpp, not the other way around.
Related
This question already has answers here:
Is bool a native C type?
(12 answers)
Closed 6 years ago.
Why am I getting a syntax error for my C header declaration?
Here is my header file, viterbi.h:
#ifndef VITERBI_H
#define VITERBI_H
void vitdec(float* , int , int , bool* );
#endif //VITERBI_H
And here is my implementation file, viterbi.c:
// viterbi.c : Defines the entry point for the console application.
//
#include "viterbi.h"
#include "math.h"
//void vitdec(float* sd, int frameLen, int rate, bool* hd);
void vitdec(float* sd, int frameLen, int rate, bool* hd)
{
//... The rest of the function
The errors from the Visual Studio 2010 compiler read:
viterbi.h(4): error C2143: syntax error : missing ')' before '*'
viterbi.h(4): error C2081: 'bool' : name in formal parameter list illegal
viterbi.h(4): error C2143: syntax error : missing '{' before '*'
viterbi.h(4): error C2059: syntax error : ')'
viterbi.h(4): error C2059: syntax error : ';'
viterbi.c(7): error C2065: 'bool' : undeclared identifier
viterbi.c(7): error C2065: 'hd' : undeclared identifier
viterbi.c(7): warning C4552: '*' : operator has no effect; expected operator with side-effect
As far as I have seen/can tell, this is valid syntax for a C declaration. If I compile viterbi.c as C++ code (viterbi.cpp), then the errors disappear. What is the syntax error?
bool is not a native C type, but for those using C99, try adding the line #include <stdbool.h>, which contains a macro that defines bool.
Since the C compiler in all Visual Studio/MSVC products uses C89, bool is not defined at all for you, as a native C type or otherwise. Workarounds include using typedef or enum to define bool. Examples are in the below link.
For more information, see: Is bool a native C type?
I have an issue compiling my code on windows.
On Unix-based systems all is working fine, but when I compile it on windows (currently with visual studio 2010 express), I'm getting the following errors:
Error 253 error C2146: syntax error : missing ';' before identifier 'N0' C:\ghost++\ghost\ohconnect.h 45
Error 254 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int C:\ghost++\ghost\ohconnect.h 45
Error 255 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int C:\ghost++\ghost\ohconnect.h 45
Error 256 error C2146: syntax error : missing ';' before identifier 'N' C:\ghost++\ghost\ohconnect.h 46
Error 257 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int C:\ghost++\ghost\ohconnect.h 46
And so on.
I think it's all relating to my header file, the class itself is made to connect to websockets:
#ifndef OHConnect_H
#define OHConnect_H
//
// OHCONNECT
//
class CTCPClient;
class CBaseGame;
class CCommandPacket;
struct OHCHeader {
unsigned header_size;
bool fin;
bool mask;
enum opcode_type {
CONTINUTATION = 0x0,
TEXT_FRAME = 0x1,
BINARY_FRAME = 0x2,
CLOSE = 8,
PING = 9,
PONG = 0xa,
} opcode;
uint64_t N0;
uint64_t N;
uint8_t masking_key[4];
};
In my .cpp file I'm using namespace std; and included <string> only for windows.
But all this didnt work so far. I didnt wanted to put the whole files into the question since they are actually long.
Here is the full source:
Headerfile
Mainfile
What did I wrong here?
The compiler doesn't know the type uint64_t and uint8_t, add:
#include <cstdint>
Note also that the trailing comma in the enum (after the definition PONG = 0xa) was only standardized in C++11, following the change made in C99. Older compilers or those running in a mode following the older 1998/2003 standard may trip over that as well.
I am using Visual C++ express 2008 try to compile code similar to below:
no problem
{
...
AVRational test = {1, 1000};
...
}
but has problem when it is as below:
{
...
AVRational test = (AVRational){1, 1000};
...
}
gave errors:
1>..\..\..\projects\test\xyz.cpp(1139) : error C2059: syntax error : '{'
1>..\..\..\projects\test\xyz.cpp(1139) : error C2143: syntax error : missing ';' before '{'
1>..\..\..\projects\test\xyz.cpp(1139) : error C2143: syntax error : missing ';' before '}'
where AVRational (ffmpeg.org library) is defined as:
typedef struct AVRational{
int num; ///< numerator
int den; ///< denominator
} AVRational;
FFmpeg come with some pre-define value such as
#define AV_TIME_BASE_Q (AVRational){1, AV_TIME_BASE}
which is used as below
av_rescale_q(seek_target, AV_TIME_BASE_Q, pFormatCtx->streams[stream_index]->time_base);
will failed to compile on Visual C++ express 2008
It seem like the same code will be compiled with no error/warning on gcc compiler. Why I get this error on VC++? Is it a C/C++ standard way to do casting on struct value? Anyway I can avoid this error while still able to use the defined AV_TIME_BASE_Q?
Use av_get_time_base_q() instead of AV_TIME_BASE_Q for C++ or VS.
This was fixed in a patch
VC++ 2013 does not allow compound literals in C++ but it allows them in C. Options:
Rename your program with a .c suffix
Switch on the /TC flag for the program that does not compile.
The other alternative if you wish to keep to C++ is to change the declaration of AV_TIME_BASE_Q in the header file
static const AVRational AV_TIME_BASE_Q = {1, AV_TIME_BASE};
Then it will be using the constant instead of the compound literal.
For compound-literals errors in C++
wrong:
this->buffer.enqueue((tone_t) { duration, frequency });
correct:
tone_t tone = { duration, frequency };
this->buffer.enqueue(tone);
second post on StackOverflow. I just have some general questions as to why my program is acting the way it is, I don't want help in completing it I was just absent from class on Friday and apparently I missed a lot.
I'm tasked to design a program that contains 3 .cpp and 2 .h files, in essence it will search and sort through arrays of strings using the bubble sort, insertion sort, selection sort methods and sequential and binary search. We are then supposed to benchmark each method to figure out which is the fastest.
I am just confused as to why the compiler keeps yelling at me, it's not making much sense I've been sitting here for about an hour fiddling around with different options or typing the code in differently but to no avail.
My header file
const int NOT_FOUND = -1;
int sequentialSearch(string a[], string needle, int length );
JohnSearch.cpp
#include "JohnSearch.h"
#include <string>
int sequentialSearch(string copied[], string needle, int length)
{
int i; // iteration variable
// look at every element to see if it is the same as needle
for( i = 0; i < length; i++ )
if( copied[i] == needle )
return i;
return NOT_FOUND;
}
TestSearch.cpp
#include "JohnSearch.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/*
** printArray(title,a,length): print out title and then the contents of array a
*/
void printArray(string title, string ref[], int length )
{
int i; // array iteration
cout << title << ": \n";
for( i = 0; i < length; i++ )
cout<<setw(20)<<ref[i]<<"\n";
}
int main(void)
{
string reference[]={"John", "Allen", "Kevin", "Elisabeth"};
const int SZ=sizeof(reference)/sizeof(reference[0]);
string copied[SZ];
printArray("Reference", reference, SZ);
// sequential search (on unsorted array)
cout<<"Search.sequential(ref,Kevin):\t"<<sequentialSearch(reference, "Kevin", SZ)<<endl;
system("Pause");
return 0;
}
Errors
johnsearch.h(2): error C2065: 'string' : undeclared identifier
johnsearch.h(2): error C2146: syntax error : missing ')' before identifier 'a'
johnsearch.h(2): error C2059: syntax error : ')'
testjohnsearch.cpp(28): error C3861: 'copyArray': identifier not found
testjohnsearch.cpp(31): error C2064: term does not evaluate to a function taking 3 arguments
johnsearch.h(2): error C2065: 'string' : undeclared identifier
johnsearch.h(2): error C2146: syntax error : missing ')' before identifier 'a'
johnsearch.h(2): error C2059: syntax error : ')'
johnsearch.cpp(7): error C2065: 'string' : undeclared identifier
johnsearch.cpp(7): error C2146: syntax error : missing ')' before identifier 'copied'
johnsearch.cpp(7): error C2374: 'sequentialSearch' : redefinition; multiple initialization
johnsearch.h(2) : see declaration of 'sequentialSearch'
johnsearch.cpp(7): error C2059: syntax error : ')'
johnsearch.cpp(8): error C2143: syntax error : missing ';' before '{'
johnsearch.cpp(8): error C2447: '{' : missing function header (old-style formal list?)
I'm obviously doing something completely and utterly wrong. I need JohnSearch.cpp for JohnSearch.h right? The forward declaration of the function in JohnSearch.h is defined in JohnSearch.cpp so I need those two files correct?
I'm just really confused. The example program we are supposed to modify has 2 .h files and 3 .cpp files. 2 of those .cpp files correspond with the 2 .h files so thats why I assumed I would also need 2 .h files and 3 .cpp files.
String is still undefined.
johnsearch.h(2): error C2065: 'string' : undeclared identifier
Your header file uses string , so you'll need to include <string>, before your declarations. You also need to qualify it as std::string since the string class resides in the std namespace
So your header file becomes:
#include <string>
const int NOT_FOUND = -1;
int sequentialSearch(std::string a[], std::string needle, int length );
(you should also use include guards in your header files)
Your JohnSearch.cpp also uses string, again, since string is in the std namespace, you'll get errors if you don't use std::string
In your TestSearch.cpp, you have a using namespace std; at the top, you could do the same in JohnSearch.cpp too, that way you can use string instead of std::string
When in doubt, simplify. You can boil the code down to something like this:
#include "JohnSearch.h"
void sequentialSearch(string needle)
{
}
and get the same error (and maybe a warning about an unused parameter).
Yes, string is a type of variable, but it's not innate in the C++ language itself, it's in one of the standard libraries, something you have to tell the compiler about:
#include "JohnSearch.h"
#include <string>
using std::string;
void sequentialSearch(string needle)
{
}
In your header file that you include, you need to have the exact same signature than your function in the cpp file.
Also dont forget to #include <string>, and then use a string like : std::string
E.g.
Int function(int number, int number2);
And in your cpp
Int function(int number, int number2)
{
// code
}
Signature is "int function(int, int)".
I have this C++11 code:
using swallow = int[];
but MSVS2013 Preview barfs on it:
error C2143: syntax error : missing ';' before '='
So I tried
typedef int[] swallow;
But that got me:
warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
So I tried reversing the typedef stuff, as I never remember (hence the reason using is so great):
typedef swallow int[];
And got:
m:\development\source\ambrosia\libambrosia\Ambrosia/utility.h++(33) : error C2144: syntax error : 'int' should be preceded by ';'
I'm already disappointed in MSVS2013. How can I write this so the MS compiler will understand this simple code?
typdef is a declaration, and follows the same syntax as a declaration:
extern int a[];
typedef int b[];
(Note that b is an incomplete type, and that a is only declared, not defined.)