Can anyone tell me why this simple function call returns the compiler error shown at bottom?
//This is a type definition that I use below to simplify variable declaration
typedef vector<int> islice;
typedef vector<islice> int2D;
// therefore int2D is of type vector<vector<int> >
// This is the function prototype in the DFMS_process_spectra_Class.hh file
int DumpL2toFile(int2D&);
// This is the type declaration in the caller
int2D L2Data;
// This is the function call where error is indicated
int DumpL2toFile(L2Data); (**line 90 - error indicated here**)
// this is the function body
int DFMS_process_spectra_Class::DumpL2toFile(int2D& L2) {
string file=sL3Path+L2Info.fileName;
fstream os;
os.open(file.c_str(), fstream::out);
os << "Pixel A-counts B-counts" << endl;
char tmp[80];
for (int i=0; i<512; ++i) {
sprintf(tmp,"%5d %8d %8d\n",L2[i][0],L2[i][1],L2[i][2]);
os << string(tmp) << endl;
}
os.close();
return 1;
}
//This is the compiler command and error
g++ -w -g -c src/DFMS_process_spectra_Class.cc -o obj/DFMS_process_spectra_Class.o
src/DFMS_process_spectra_Class.cc:
In member function 'int DFMS_process_spectra_Class::processL2()':
src/DFMS_process_spectra_Class.cc:90: error:
cannot convert 'int2D' to 'int' in initialization
Why is the compiler confusing int2D& with int? The call, function prototype, and function are consistently int2D type!!
//Here is my compiler version
i686-apple-darwin11-llvm-g++-4.2 on Mac OS X 10.8.3
This by the way is the same error I get on my Linux box with g++ 4.3
Thanks for any help, Mike
// This is the function call where error is indicated
int DumpL2toFile(L2Data); (**line 90 - error indicated here**)
That's not a function call! Assuming that this line occurs inside a function body (which is not clear from your code), the function call would be:
DumpL2toFile(L2Data); // No int
OP, that's all you need to know. But if you are curious, the compiler is parsing your statement as if it were
int AnyOldIdentifier(L2Data);
which is a declaration of an int variable called AnyOldIdentifier, initialised to the value L2Data. And it can't initialise an int to L2Data, because L2Data is an int2D, not an int.
You have syntax error around this line:
// This is the function call where error is indicated
int DumpL2toFile(L2Data); (**line 90 - error indicated here**)
If you call DumpL2toFile. you don't need the return type anymore. this way, compiler treats it as function declaration, however, L2Data is not a type, it is an object of int2D, this triggers compiling error
Meanwhile, compile error says error insde processL2() function, while you did not post code of this part.
Related
I'm trying to compile the following C++ code on Visual Studio Code, using the Mac clang compiler.
#include <iostream>
int main() {
int x { 5 };
std::cout << x;
return 0;
}
However, this returns an error, on the line of the list initialization: int x{ 5 };. Specifically, it says I need to insert a semicolon after the x.
I don't get what's wrong with this code, it works fine on an online compiler. How do I fix this?
Running man clang in the Terminal and skimming through, I found this:
The default C++ language standard is gnu++14.
UPDATE: I ran clang++ main.cpp in the compiler and it returned that semicolon error. This isn't a problem with VSCode, so I'll remove that tag.
Here's the error:
main.cpp:3:10: error: expected ';' at end of declaration
int x { 5 };
^
;
1 error generated.
I found this situation in my recent project. I wanna ask if it is designed as so, what's the underlying mechanism, and how is it useful?
Although I know the subscript in parameters list is somehow useless to compiler, but in my situation, it might be better to raise an error and stop compile.
The source code:
#include <cstdio>
template<typename Type>
class A{
public:
// passes compile, although it is not declared
void print(int data[Type::len]) { // Line 7
printf("%d\n", data[0]);
}
// error: not declared
// void print(int data[dummy]) {
// printf("%d\n", data[0]);
// }
};
// error: not declared
// void print(int data[A<double>::len]) {
// printf("%d\n", data[0]);
// }
int main() {
A<int> a;
int x[12] = { 0 };
a.print(x); // Line 23
return 0;
}
Compile command using gcc 11.3.0:
g++ -o a a.cpp -Wall -Wextra
No compile output, program prints a "0" and exits with 0.
But in msvc 19.33.31630, it raises C2825, C2510 on line 7, and C2670 on line 23.
There should be an error and that there isn't one is a known bug in GCC.
If the array bound Type::len was valid, then the type int[Type::len] in the function parameter would be rewritten to int* in the instantiation, as is always the case for array types in function parameters. So the actual value of the array bound will not matter from there on out.
However, if Type::len is not a valid constant expression with a suitable value for an array bound, then substitution should fail and the program should be ill-formed. In case of T=int, Type::len is not a valid expression at all and so it should fail to compile (or at least issue some diagnostic). This should happen already at A<int> a; (which causes implicit instantiation of A<int> including A<int>::print's declaration), even if no actual call to print is present.
It seems GCC is performing the rewriting step too early without verifying that the expression in the array bound is actually valid in the substitution.
GCC has a meta-bug report with multiple linked individual bug reports related to this here.
I have run this code on another Linux environment where it works, but when I run this code on my machine it shows an error.
The code is:
void *functionC(void* ptr)
{
dint* pointer=(int*)ptr;
pthread_mutex_lock( &mutex1 );
int i;
for( i=pointer[0]; i <= pointer[1]; i++ )
{
sum += myarray[i];
}
pthread_mutex_unlock( &mutex1 );
}
The error I got in my machine is:
aftab#aftab-VirtualBox:~/Downloads$ gcc -o out done1.c -lpthreads
done1.c: In function ‘functionC’:
done1.c:59:2: error: unknown type name ‘dint’
dint* pointer=(int*)ptr;
The error that gcc is complaining about is that there is an unknown type name ‘dint’.
There is no basic type dint in c++, so it must be declared somewhere.
In the version of the code you are copying from there is probably either a typedef or a header file that you haven't included.
From a little looking, I don't see any reference to a dint in pintos, so it's probably defined somewhere in the original file.
Look for a line like:
typdef int dint;
The following code :
#include <iostream>
using namespace std;
int test()
{
cout<<"question \n";
return 0;
}
int main(){
cout<<test;
}
Output:
question
1
The following code gives 1 everytime I run but I am expecting the output to be 0.
Whereas when I replace the test by test() then I get the expected output. Not sure why is this happening. Please suggest and comment if any rule behind it.
C++ always requires you to use parentheses to call functions. If you omit them, it thinks you want to know the address of the function instead of calling it. The address is then considered to be a (truthy) boolean and gets output as 1. This warning (from gcc -Wall) explains it well:
x.cpp: In function ‘int main()’:
x.cpp:9:11: warning: the address of ‘int test()’ will always evaluate as ‘true’ [-Waddress]
cout<<test;
^
std::cout << test;
Outputs 1 because test is a function pointer, it will be converted to bool with std::cout.
I'm writing software to control a bladeRF radio card but I'm running into a strange compiler/linker error that I haven't been able to figure out. My code uses several functions and data structures defined in the library, libbladeRF, but for some reason I can't reference to one specific function.
However, if I modify the call with an improper argument type, g++ will throw an error to let me know that it doesn't conform to the definition, which seems to tell me that the linker is actually able to locate the reference.
What am I missing?
Initial error:
$ g++ bladeRF_test.cpp -o bladeRF_test -lbladeRF
/tmp/ccTWZzdJ.o: In function `enable_xb300()':
bladeRF_test.cpp:(.text+0x36a): undefined reference to `bladerf_xb300_set_amplifier_enable'
Code excerpt:
#include <iostream>
#include <string>
#include <libbladeRF.h>
using namespace std;
...
int set_xb300_pa(bool enable) {
bladerf_xb300_amplifier amp = BLADERF_XB300_AMP_PA;
if ( bladerf_xb300_set_amplifier_enable(dev, amp, enable) ) {
// Print error message
return -1;
} else {
// Print success message
return 0;
}
}
...
Function arguments changed from (dev, amp, enable) to (&dev, amp, enable):
$ g++ blade_hello.cpp -o blade_hello -lbladeRF
blade_hello.cpp: In function ‘int set_xb300_pa()’:
blade_hello.cpp:62:59: error: cannot convert ‘bladerf**’ to ‘bladerf*’ for argument ‘1’ to ‘int bladerf_xb300_set_amplifier_enable(bladerf*, bladerf_xb300_amplifier, bool)
^
In file included from blade_hello.cpp:4:0:
/usr/local/include/libbladeRF.h:2226:15: note: declared here
int CALL_CONV bladerf_xb300_set_amplifier_enable(struct bladerf *dev,
^