I'm trying to write a simple program to calculate numerical approximations with Euler's method and every complier I've used hasn't printed anything. Codeblocks is running an error but I think that is because the compiler isn't set up right. xCode will build it but nothing happens. When I run g++ Euler.cpp I get:
Euler.cpp:1: error: expected constructor, destructor, or type conversion before ‘<’ token
Euler.cpp: In function ‘int main()’:
Euler.cpp:13: error: ‘cin’ was not declared in this scope
Euler.cpp:19: error: ‘cout’ was not declared in this scope
Euler.cpp:19: error: ‘endl’ was not declared in this scope
I usually never have problems with simple c++ programs and fear it's something very obvious.
//
// Euler.cpp
// Numerical Approximations (Euler's Method)
//
// Created by XXXXXXXXXXXX on 6/18/12.
// Copyright (c) 2012 University of Kansas Department of Mathematics. All rights reserved.
//
#include <iostream>
using namespace std;
int main ()
{
int N=4;
//cout<<"Number of steps (N):";
//cin>>t;
float h=0.1;
//cout<<endl<<" Step size (h):";
cin>>h;
float y0=1;
//cout<<endl<<"y(0)=";
//cin>>y0;
cout<<"test!"<<endl;
float data[N][4];
int n=0;
data[0][2] = y0;
while (n<N){
data[n][0]=n;
if(n>0){
data[n][2]=data[n-1][3];
}
data[n][1]=h*n;
data[n][3] = data[n][2] + ((3 + data[n][1] - data[n][2])*h);
n++;
cout<<"n="<<n<<". tn="<<data[n][1]<<". y(n)="<<data[n][2]<<". y(n+1)="<<data[n][3] <<"."<<endl;
}
return 0;
}
It's probably something obvious but I don't see it.
It's not finding the iostream header. Do you see an error message reading something like "couldn't find header iostream" ?
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'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,
^
I am a bit new to programming as you can probably tell from my prior question(s). I was wondering if anyone could help me with this recent problem I've had. I am trying to compile a script main.cpp using g++ but I get the following errors:
Donny#Donny-PC /cygdrive/c/Users/Donny/Desktop/equation/equations/equations
$ g++ main.cpp -o don.exe
main.cpp:3:11: error: ‘::main’ must return ‘int’
void main(){
^
main.cpp: In function ‘int main()’:
main.cpp:36:22: error: ‘pow’ was not declared in this scope
float n=pow(10.0,9.0);
^
main.cpp:43:27: error: ‘sin’ was not declared in this scope
float R56=(lb1/sin(theta1)) * ((tan(theta1))-theta1) + (lb2/sin(theta1)) * ((tan(theta1))-theta1) +
^
main.cpp:43:44: error: ‘tan’ was not declared in this scope
float R56=(lb1/sin(theta1)) * ((tan(theta1))-theta1) + (lb2/sin(theta1)) * ((tan(theta1))-theta1) +
^
main.cpp:48:40: error: ‘cos’ was not declared in this scope
d*((pow(tan(theta1),2))/cos(theta1)) +
^
The weird thing is that this code works when compiled with microsoft visual studio 2010 C++. Any help would be greatly appreciated!
EDIT:
So, the fixed a lot of the errors shown above, but I am still having a little difficulty fixing the void main error. Here is how my code looks:
#include<iostream>
#include<cmath>
using namespace std;
void main(){
float r, i, f, beta, alpha;
cout<<"Enter value of R : ";............
Any help or examples would be greatly appreciated.
The first error should be self-explanatory. The standard says that the main function must return int but you have declared it as void. Return 0 from your main function to indicate normal termination. The Microsoft compiler is not as strict on this point.
All your remaining errors can be remedied by using #include <math.h>.
I noticed that g++ complain a bit too strictly about crossed initialization and I wonder why these false-positive errors could not be removed just by looking at the SSA form of the program while compiling.
Let me give a very simple example:
#include <cstdlib>
int main ()
{
goto end;
int i = 0; // unused variable declaration
end:
return EXIT_SUCCESS;
}
When compiled with g++ -Wall -Wextra -o example1 example1.cc (g++ 4.8.1), the compiler gives the following error message:
example1.cc: In function ‘int main()’:
example1.cc:10:2: error: jump to label ‘end’ [-fpermissive]
end:
^
example1.cc:6:8: error: from here [-fpermissive]
goto end;
^
example1.cc:8:7: error: crosses initialization of ‘int i’
int i = 0;
^
example1.cc:8:7: warning: unused variable ‘i’ [-Wunused-variable]
So, it will raise an error where there is actually no risk because the variable is unused (the compiler obviously has both information and cannot combine it to deduce that the error is a false-positive).
More strange, I hoped that LLVM was more efficient at analyzing a program. So, I tried clang++ (LLVM) on this simple example with clang++ -Wall -Wextra -o example1 example1.cc (clang++ 3.4). And, I got about the same error message:
example1.cc:8:7: warning: unused variable 'i' [-Wunused-variable]
int i = 0;
^
example1.cc:6:3: error: goto into protected scope
goto end;
^
example1.cc:8:7: note: jump bypasses variable initialization
int i = 0;
^
1 warning and 1 error generated.
So, I am pretty sure that I am missing something important here, a problem that make the detection of this false-positive harder than I though. But, I do not know what is it. Or maybe, the C++ specification specifically says that it must be like this.
If somebody has an idea, feel free to share !
Edit: I also compiled the exact same code in C (gcc or clang), and it went fine just with the warning about i being an unused variable. So, it reinforce the fact that this is more likely linked to the specification of C++ and not a problem detecting this issue at compile time.
There is nothing wrong with the compilers. Your code is ill-formed according to the standard.
In your particular case, the requirement of the standard may not be necessary and the jump could be allowed and the compiler could create valid code. However, this is only because the initialisation of the variable int i has no side effects.
You can make your code valid by simply enclosing the jumped section in its own scope:
#include <cstdlib>
int main ()
{
goto end;
{
int i = 0; // unused variable declaration
}
end:
// cannot use i here, as it's not defined.
return EXIT_SUCCESS;
}
This is disallowed because potentially you'd call destructors for objects that aren't properly constructed. Admittedly, int doesn't have constructor or destructor, but it's making it "fair" for all types of objects. And technically, something at label end: could be using i, and by making the rule strict, it prevents the machine having to check every single code-path (which becomes a "halting problem").
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.