Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
void func_print(int value) {
std::cout << “int” << std::endl;
}
void func_print(void* value) {
std::cout << “void” << std::endl;
}
int main() {
func_print(0);
func_print(NULL);
}
I cant seem to find. My compiler may be broken because it gives me stray errors cant find it what is wrong.
g++: error: -E or -x required when input is from standard input
c.cpp:3:5: error: stray ‘\342’ in program
std::cout << “int” << std::endl;
^
c.cpp:3:5: error: stray ‘\200’ in program
c.cpp:3:5: error: stray ‘\234’ in program
c.cpp:3:5: error: stray ‘\342’ in program
c.cpp:3:5: error: stray ‘\200’ in program
c.cpp:3:5: error: stray ‘\235’ in program
c.cpp:6:5: error: stray ‘\342’ in program
std::cout << “void” << std::endl;
^
c.cpp:6:5: error: stray ‘\200’ in program
c.cpp:6:5: error: stray ‘\234’ in program
c.cpp:6:5: error: stray ‘\342’ in program
c.cpp:6:5: error: stray ‘\200’ in program
c.cpp:6:5: error: stray ‘\235’ in program
c.cpp: In function ‘void func_print(int)’:
c.cpp:3:21: error: expected primary-expression before ‘int’
std::cout << “int” << std::endl;
^
c.cpp: In function ‘void func_print(void*)’:
c.cpp:6:21: error: expected primary-expression before ‘void’
std::cout << “void” << std::endl;
^
c.cpp: In function ‘int main()’:
c.cpp:11:20: error: call of overloaded ‘func_print(NULL)’ is ambiguous
func_print(NULL);
^
c.cpp:2:6: note: candidate: void func_print(int)
void func_print(int value) {
^
c.cpp:5:6: note: candidate: void func_print(void*)
void func_print(void* value) {
All errors here explained with messages though i don't get it exactly what is wrong.
Which functions get called when you send 0 as argument
The call is ambiguous. Neither function is preferred by the overload resolution, since 0 is both an int literal, as well as a pointer literal. An ambiguous call makes the program ill-formed, so a compiler is not required to accept it. This is what the compiler told you:
error: call of overloaded ‘func_print(NULL)’ is ambiguous
std::cout << “void” << std::endl;
This is wrong, because “ (Left Double Quotation Mark) is not a valid character there. You've most likely attempted to write a string literal. A string literal uses the " (Quotation Mark) character, which is similar. This is what the compiler told you:
error: expected primary-expression before ‘void’
It doesnt have any include or headers it should compile by its own (supposedly)
Your supposition is wrong. Besides the problems mentioned earlier, std::cout (or anything else from the std namespace) cannot be used without including standard headers.
Related
This question already has answers here:
Compilation error: stray ‘\302’ in program, etc
(12 answers)
Closed last month.
I'm getting these errors in my program after pasting in some code:
showdata.cpp:66: error: stray ‘\342’ in program
showdata.cpp:66: error: stray ‘\200’ in program
showdata.cpp:66: error: stray ‘\235’ in program
showdata.cpp:66: error: stray ‘\’ in program
showdata.cpp:66: error: stray ‘\342’ in program
showdata.cpp:66: error: stray ‘\200’ in program
showdata.cpp:66: error: stray ‘\235’ in program
showdata.cpp:67: error: stray ‘\342’ in program
showdata.cpp:67: error: stray ‘\200’ in program
showdata.cpp:67: error: stray ‘\235’ in program
showdata.cpp:67: error: stray ‘\’ in program
showdata.cpp:67: error: stray ‘\342’ in program
showdata.cpp:67: error: stray ‘\200’ in program
showdata.cpp:67: error: stray ‘\235’ in program
Here are the two lines that are causing the errors.
size_t startpos = str.find_first_not_of(” \t”);
size_t endpos = str.find_last_not_of(” \t”);
How can I fix this?
The symbol ” is not ". Those are called 'smart quotes' and are usually found in rich documents or blogs.
The lines
size_t startpos = str.find_first_not_of(” \t”);
size_t endpos = str.find_last_not_of(” \t”);
have some "special" kind of double quotes, try the following:
size_t startpos = str.find_first_not_of(" \t");
size_t endpos = str.find_last_not_of(" \t");
This sort of error message, error: stray ‘\xyz’ in program, can appear with any other character or symbol that is not recognized by the compiler as a legal one.
Sharing my personal experience:
- bool less<const char∗>(const char∗ a, const char∗ b)
- bool less<const char*>(const char* a, const char* b)
The former one is copy-pasted from a PDF file. It doesn't compile.
The latter one compiles as expected.
You can use the sed command to fix these issues.
This will give you a quick preview of what will be replaced.
sed s/[”“]/'"'/g File.txt
This will do the replacements and put the replacement in a new file called WithoutSmartQuotes.txt.
sed s/[”“]/'"'/g File.txt > WithoutSmartQuotes.txt
This will overwrite the original file.
sed -i ".bk" s/[”“]/'"'/g File.txt
When I'm trying to run a simple hello world script it sends this error message, Can anyone help me solving this issue?
[Running] cd "c:\Users\NickT\OneDrive\Documents\C++ Tutorial\" && g++ helloworld.cpp -o helloworld && "c:\Users\NickT\OneDrive\Documents\C++ Tutorial\"helloworld
helloworld.cpp: In function `int main()':
helloworld.cpp:9: error: expected primary-expression before "msg"
helloworld.cpp:9: error: expected `;' before "msg"
helloworld.cpp:11: error: expected primary-expression before "const"
helloworld.cpp:11: error: expected `;' before "const"
helloworld.cpp:16: error: expected primary-expression before '}' token
helloworld.cpp:16: error: expected `)' before '}' token
helloworld.cpp:16: error: expected primary-expression before '}' token
helloworld.cpp:16: error: expected `;' before '}' token
[Done] exited with code=1 in 0.233 seconds
This is the written code
#include <iostream>
int main(){
std::cout << "Hello World" << std::endl;
return 0;
}
I revised the code you have posted as well as added a piece of code to utilize the namespace std. Analyzing the output from your compiler it does seem that you may have not added a semi colon. Alternatively, it's not necessary to reference the std library when calling it's functions but it's up to your preference. It doesn't seem though to be an issue with your code either. Can you post the actual code you've attempted to compile because there doesn't seem to be an issue with this piece.
Revised Code:
#include <iostream>
using namespace std;
int main() {
std::cout << "Hello World" << std::endl;
return 0;
}
This question already has answers here:
error: ‘i’ does not name a type with auto [duplicate]
(2 answers)
Closed 6 years ago.
#include <iostream>
#include <tuple>
#include <string>
using namespace std;
int main(){
tuple<string, string, string> x;
x = make_tuple("hi", "a", "b");
cout << get<0>(x) << endl << endl;
}
I've been having difficulties with my program, so I wrote a simpler one and even this does not work. I do not understand why there is a problem after reviewing the documentation several times. It also compiles fine on XCode but for some reason breaks down on g++.
Here is the full error message:
test.cpp:6:3: error: use of undeclared identifier 'tuple'
tuple x;
^
test.cpp:6:9: error: unexpected type name 'string': expected
expression
tuple x;
^
test.cpp:7:3: error: use of undeclared identifier 'x'
x = make_tuple("hi", "a", "b");
^
test.cpp:7:7: error: use of undeclared identifier 'make_tuple'
x = make_tuple("hi", "a", "b");
^
test.cpp:8:11: error: reference to overloaded function could not be
resolved; did you mean to call it? cout << get<0>x << endl << endl;
The command I am using is g++ test.cpp
Try #include <string>.
Possibly (depending on your version og gcc) you also need -std=c++11 on the command line.
The tuple is fine; what you're trying to make it a tuple of is not.
You did not #include <string>!
Thus the word "string" means nothing to your compiler, and it has no idea what you want it to do. It can't even tell that you meant it to be a type, so it can't tell that by the word "tuple" you meant "std::tuple". So on, and so forth…
I am working on adapting some C++ code from Windows to Linux to compile as mex in Matlab. The program is designed to connect a data glove via a virtual serial port. I've never worked much at all with C++ before, so the solution this issue could very well be quite simple. In a larger file, I am getting issues caused by the following try/catch:
try
{
// You can tell which com port to use by checking windows settings under hardware
glove = new CyberGlove(portName, (int) *baudRate); // baudrate 115200
/* Return CyberGlove object */
mxArray* result = (mxArray*) mxMalloc(sizeof(CyberGlove));
*reinterpret_cast<CyberGlove*>(result) = *glove;
plhs[0] = result;
}
catch(std::runtime_error err)
{
std::cout << "Error: " << err.what() << std::endl;
}
delete glove;
When I try to compile it in Matlab, I get the following errors:
InitCyberGlove.cpp: In function ‘void mexFunction(int, mxArray**, int, const mxArray**)’:
InitCyberGlove.cpp:43:11: error: expected type-specifier
InitCyberGlove.cpp:43:30: error: expected unqualified-id before ‘err’
InitCyberGlove.cpp:43:30: error: expected ‘)’ before ‘err’
InitCyberGlove.cpp:43:30: error: expected ‘{’ before ‘err’
InitCyberGlove.cpp:43:30: error: ‘err’ was not declared in this scope
InitCyberGlove.cpp:43:33: error: expected ‘;’ before ‘)’ token
I don't think it's specifically related to the CyberGlove stuff so much as my general lack of knowledge of C++ and how exceptions work.
You need to #include <stdexcept> where std::runtime_error is declared.
This question already has answers here:
Compilation error: stray ‘\302’ in program, etc
(12 answers)
Closed last month.
I'm getting these errors in my program after pasting in some code:
showdata.cpp:66: error: stray ‘\342’ in program
showdata.cpp:66: error: stray ‘\200’ in program
showdata.cpp:66: error: stray ‘\235’ in program
showdata.cpp:66: error: stray ‘\’ in program
showdata.cpp:66: error: stray ‘\342’ in program
showdata.cpp:66: error: stray ‘\200’ in program
showdata.cpp:66: error: stray ‘\235’ in program
showdata.cpp:67: error: stray ‘\342’ in program
showdata.cpp:67: error: stray ‘\200’ in program
showdata.cpp:67: error: stray ‘\235’ in program
showdata.cpp:67: error: stray ‘\’ in program
showdata.cpp:67: error: stray ‘\342’ in program
showdata.cpp:67: error: stray ‘\200’ in program
showdata.cpp:67: error: stray ‘\235’ in program
Here are the two lines that are causing the errors.
size_t startpos = str.find_first_not_of(” \t”);
size_t endpos = str.find_last_not_of(” \t”);
How can I fix this?
The symbol ” is not ". Those are called 'smart quotes' and are usually found in rich documents or blogs.
The lines
size_t startpos = str.find_first_not_of(” \t”);
size_t endpos = str.find_last_not_of(” \t”);
have some "special" kind of double quotes, try the following:
size_t startpos = str.find_first_not_of(" \t");
size_t endpos = str.find_last_not_of(" \t");
This sort of error message, error: stray ‘\xyz’ in program, can appear with any other character or symbol that is not recognized by the compiler as a legal one.
Sharing my personal experience:
- bool less<const char∗>(const char∗ a, const char∗ b)
- bool less<const char*>(const char* a, const char* b)
The former one is copy-pasted from a PDF file. It doesn't compile.
The latter one compiles as expected.
You can use the sed command to fix these issues.
This will give you a quick preview of what will be replaced.
sed s/[”“]/'"'/g File.txt
This will do the replacements and put the replacement in a new file called WithoutSmartQuotes.txt.
sed s/[”“]/'"'/g File.txt > WithoutSmartQuotes.txt
This will overwrite the original file.
sed -i ".bk" s/[”“]/'"'/g File.txt