C++ code problem with strcpy clone function [duplicate] - c++

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

Related

Getting an weird error in vscode when i'm trying to run a code

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;
}

Which functions get called when you send 0 as argument [closed]

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.

c++ Error with literal initialize pointer with curly brace [duplicate]

This question already has answers here:
How do I activate C++ 11 in CMake?
(17 answers)
Closed 4 years ago.
I'm learning new of c++.
In my text book, there is very simple example this.
int* ip = nullptr; // (ok)
int* ip1{}; // (error)
int* ip2{nullptr}; // (error)
unique_ptr<double> dp{new double}; // (error)
But I got this error with build it.
/Users/bsdo/projects/CPlus/modernbook/include/basis.cpp:81:30: error: expected ';' at end of declaration
unique_ptr<double> dp{new double};
^
;
In file included from /Users/bsdo/projects/CPlus/modernbook/main.cpp:2:
/Users/bsdo/projects/CPlus/modernbook/include/basis.cpp:81:30: error: expected ';' at end of declaration
unique_ptr<double> dp{new double};
^
;
/Users/bsdo/projects/CPlus/modernbook/main.cpp:10:13: error: expected ';' at end of declaration
int* ip1{nullptr};
^
;
Why only curly brace literal throw error??
My env is Mac book pro 2015, Cmake-3.11, Apple LLVM version 9.1.0 (clang-902.0.39.1).
Just make cmake compile c++11..
Configuration is write this line in CmakeLists.txt
set (CMAKE_CXX_STANDARD 11)

Using Octave in C++

I am new in both Octave and C++ ! I want to use Octave in C++ and found this webpage. I started with the upper code and tried to run it. Unfortunately there is no document on this matter and I encountered with a lot of errors. I first used msvc 2010 and found that the I should use gcc!! Here are what I'v done so far:
Download Octave from this link, (I download octave-4.0.0_0.zip)
I tried to compile this code:
int main (void)
{
std::cout << "Hello Octave world!\n";
int n = 2;
Matrix a_matrix = Matrix (n, n);
for (octave_idx_type i = 0; i < n; i++)
for (octave_idx_type j = 0; j < n; j++)
a_matrix(i,j) = (i + 1) * 10 + (j + 1);
std::cout << a_matrix;
return 0;
}
I also included: 1) iostream and 2) octave directory: N:\octave-4.0.0\include\octave-4.0.0\octave\oct.h
I got this error in msvc10:
1>n:\octave-4.0.0\include\math.h(169): error C2065: 'asm' : undeclared identifier
1>n:\octave-4.0.0\include\math.h(169): error C2146: syntax error : missing ';' before identifier 'volatile'
1>n:\octave-4.0.0\include\math.h(169): error C2143: syntax error : missing ')' before ':'
1>n:\octave-4.0.0\include\math.h(169): error C2059: syntax error : ')'
and this error in codeblocks using gcc:
C:/Users/Sam/Documents/codeblock_C++/testOctave/main.cpp:19: undefined reference to operator<<(std::ostream&, Matrix const&)'
obj\Debug\main.o: In functionZN10dim_vector11make_uniqueEv':
N:/octave-4.0.0/include/octave-4.0.0/octave/dim-vector.h:134: undefined reference to __sync_add_and_fetch_4'
obj\Debug\main.o: In functionZN10dim_vectorD1Ev':
N:/octave-4.0.0/include/octave-4.0.0/octave/dim-vector.h:286: undefined reference to __sync_add_and_fetch_4'
obj\Debug\main.o: In functionZN15octave_refcountIiEmmEv':
N:/octave-4.0.0/include/octave-4.0.0/octave/oct-refcount.h:72: undefined reference to __sync_add_and_fetch_4'
obj\Debug\main.o: In functionZN5ArrayIdEC2ERK10dim_vector':
N:/octave-4.0.0/include/octave-4.0.0/octave/Array.h:184: undefined reference to dim_vector::safe_numel() const'
obj\Debug\main.o:main.cpp:(.rdata$_ZTV6Matrix[__ZTV6Matrix]+0x10): undefined reference toArray::resize_fill_value() const'
obj\Debug\main.o:main.cpp:(.rdata$_ZTV7NDArray[__ZTV7NDArray]+0x10): undefined reference to Array<double>::resize_fill_value() const'
obj\Debug\main.o:main.cpp:(.rdata$_ZTV6MArrayIdE[__ZTV6MArrayIdE]+0x10): undefined reference toArray::resize_fill_value() const'
obj\Debug\main.o:main.cpp:(.rdata$_ZTV5ArrayIdE[__ZTV5ArrayIdE]+0x10): undefined reference to `Array::resize_fill_value() const'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
I really appreciate any help!
Octave > 4.0 is using msys and gcc in thein windows, but you don't really need to know that in order to get it to compile.
Looking at what you are trying to do, your are using a IDE other than octave to try to compile the code without then including the octave paths and octave libraries correctly.
The easiest way is to use octave to do it.
Firstly you will need to include the octave header, and since you are using cout, iostream:
#include <octave/oct.h>
#include <iostream>
Then from octave us the mkoctfile command with the --link-stand-alone option as shown in the link you referenced.
mkoctfile --link-stand-alone main.cpp

Stray '\342' in C++ program [duplicate]

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