Tag Archives: C

Various errors (c + +)

OS X EI Capitan [10.11.6]
Xcode [8.0]
C++
Code:

#include< iostream>
#include< math.h>
#include”MyComplex.h”
usingnamespacestd;
int main(int argc,constchar * argv[]) {
//insert code here…
MyComplex c1;
MyComplex c2.
Cin & gt; > c1;
C2 = c1;
Cout & lt; < c1 < < endl;
Cout & lt; < c2 < < endl;
MyComplex c3.
C3 = c1 + c2; //error1:No viable overloaded ‘=’
Cout & lt; < c1+c2 < < Lendl; //error2: No matching constructor for initialization of ‘MyComplex’
Cout & lt; < c1-c2 < < Lendl; //error
Cout & lt; < c1/c2 < < Lendl; //error
Cout & lt; < c1*c2 < < Lendl; //error
    
Return0;
}

So how is the overloading of = defined?Look at the code.

MyComplex& operator = (MyComplex & rhs){
Real = RHS. GetReal ();
Imaginary = RHS. GetImaginary ();
Return * this;
}

It doesn’t seem to be a problem. By looking at http://www.cplusplus.com/forum/general/153830/ to find the reason of the error:
A temporary object can’t bind to A non-const reference.
(A temporary object cannot be bound to an extraordinary reference, that is, the bound reference must be constant)
Therefore, the parameter of the overloaded = function must be const. (Such a covert mistake is more than enough to cry about…)
Error1 disappears after adding const:

MyComplex& operator = (constMyComplex & rhs){
Real = RHS. GetReal ();
Imaginary = RHS. GetImaginary ();
Return * this;
}
Error2 has the same error cause.

Friend ostream& operator< < (ostream& os,const MyComplex & c);
Const is very important! Want to! !!! After the correction, the errors disappeared

error: `cout’ was not declared in this scope

Error compiling C++ using GCC under Linux:

#include< iostream>
int main()
{
cout < < “Hello World!” < < endl;
return 0;
}

compiler error:
$g++ s.> s.bbpp: In function ‘int main(int, char**)’:
The



s.cpp:12: error: `cout’ was not declared in this scope

s.cpp:12: error: `endl’ was not declared in this scope

The reason:

C++ 1998 requires cout and endl to be called using ‘STD ::cout’ and’ STD ::endl’ formats, or using namespace STD;

Revised:

#include< iostream>

int main()

{
std::cout < < “Hello World!” < < std::endl;
return 0;
}

or

#include< iostream>

using namespace std;

int main(int argc, char *argv[])

{

cout < < “hello world” < < endl;
return 0;
}

Compile through.

error C2057: expected constant expression (Can the size of an array in C language be defined when the program is running?)

Can the size of an array be defined at run time?
no. In the definition of an array, the size of the array must be known at compile time, not at run time. For example, if I is a variable, you cannot use I to define the size of an array:
char array[I]; Notvalidc */
supports this definition in some languages, but not in C. If C supported this definition, the stack would become more complex, the overhead of calling functions would be greater, and the program would run significantly slower.
if the size of the array is known at compile time, even if it is a very complex expression, you can define it as long as it can be calculated at compile time. If you want to use an array whose size is not known until the program is running, you can specify a pointer and call the malloc() or calloc() functions to allocate memory space for the array from the heap. Here is an example of an argv array that is copied to the main() function:

[cpp]
view plain
copy

    7.15 cases in row to determine the size of the array, use the pointer and the malloc ()/* A silly program that copies the argv array and all the pointed – to strings. Just for fun, it also deallocates all the copies. */# include & lt; Stdlib. H> # include & lt; String. H> Int main (int arg c, char * * argv) {char * * new_argv; Int I;/* Since argv [0] through argv [arg c] are all valid, the program needs to the allocate room for arg c + 1 Pointers. */new_argv = (char * *) calloc (arg c + l, sizeof (char *));/* or malloc ((arg c + 1) * sizeof (char *)) */printf (” allocated room for % d Pointers starting at % P \ n “, arg c + 1, new_argv);/* now copy all the strings more (argv [0] through argv [arg c – l]) */for (I = 0; i< argc; + + I) {/ * make room for ‘\ 0’ at end, too */new_argv [I] = (char *) malloc (strlen (argv [I]) + l); Strcpy (new_argv [I], argv [I]); Printf (” % d bytes allocated for new_argv [% d] at % P “, “copied \” % s \ \ “n”, strlen (argv [I]) + l, I, new_argv [I], new_argv [I]); } new_ argv [arg c] = NULL:/* To deallocate everything, get rid of the strings (in any order), then the array of Pointers. If you free the array of poiners first, you lose all the reference To t (I = 0); i< argc; + + I) {free (new_argv [I]); Printf (” freed new_argv [% d] at % P \ n “, I, new_argv [I]); Argv [I] = NULL; * * Habit, see note at the end of this example */} free(new_argv); Printf (” Freed new_argv itself at %P\n”, new_argv); Return 0; /* see 16.4 */}

Note: Why does example 7.5 assign NULL after freeing each element in the new_argv array?This is a habit formed on the basis of long practice. After a pointer is released, you can no longer use the data it originally pointed to, or the pointer is “suspended” and no longer points to any useful data. If a pointer is assigned NULL immediately after it is released, the program will not make an error even if it USES the pointer again. Of course, the program may indirectly refer to the null pointer, but such errors can be detected in time while debugging the program. In addition, a
program may still have some original copies of the pointer pointing to the portion of memory that has been freed, which is natural in C programs. In short, although this habit doesn’t solve all problems, it does help.

matlab Error Subscript indices must either be real positive integers or logicals.

Matlab errors: Subscript Indices must either be real positive integers or Logicals.Subscript index must be of positive integer type or logical type

Cause of error: in the process of accessing the matrix (including vector, two-dimensional matrix, multidimensional array, same below), the index of the subscript either starts at 0 or has a negative number. Note: The syntax of MATLAB stipulates that the index of the matrix starts from 1, which is different from the habit of programming languages such as C.

Solution: Debug the program yourself and fix subscripts that are zero or negative.

I made a mistake when I was writing the program. I should have done the transpose of the matrix, but I forgot…

Bus error: 10

Recently, I wrote a small program with many analogisms. For the convenience of compilation, I wrote a Makefile specially for source code and test code.
In the process of debugging, a “Bus error: 10” suddenly appeared, which was quite unexpected. Why did such a spectacular problem occur?
The starting address of type int data must be a multiple of 4, otherwise it will cause the bus error mentioned above. With this in mind, I searched for a long time but couldn’t find any address misaligned.
When I compiled again, I found something fishy about the compilation process. The Makefile compiles the source file “.c/.cpp “into”.o “files (binaries), and then links these”.o “files to target files (executables, libraries, etc.). If some of these files are modified, the Makefile reduces compilation time by detecting the timestamp and compiling only the files that are modified later. So the question is, what happens if you change the header file?Examples are as follows:
Suppose you are going to modify two files, classA.h classa.cpp, add a member variable to the header file, and do the processing in the source file.
The two files classb.cpp classC.cpp will include the classA.h file. Because of the include type, the actual code in the file has been written to the source file and compiled into a “.o “file.
When the modification is completed, there is no change in classb. CPP classc. CPP, so when comparing the timestamp, determine that classb. CPP classc. CPP does not need to be compiled again. However, the header file classA.h of the classb. CPP classC.cpp file has really changed, so when the data is accessed, the address offset of its members will be greatly changed, no error can be found at compile time, but the execution will explode…
 
Solution: To sum up, it is not difficult to find the problem. The header files held before and after the code are scattered, resulting in running errors. It is only necessary to compile the classes of the modified header files that depend on, and the simple solution is to compile the current project after clean.
This blog is just for the sake of documenting a childish problem you’ve committed: if the header file changes, recompile it all or die…
The above!
 
So the question is, can the Makefile add dependencies on the source file’s head file?Detects the timestamp of the dependency header file and the “.o “file and compiles them. Make a note of it and study it later.

codeblocks ERROR: You need to specify a debugger program in the debuggers’s settings.

Codeblocks error:
Debug
ERROR: You need to specify a debugger program in the debuggers’s settings.
(For MinGW compilers, it’s ‘gdb.exe’ (without the quotes))
(For MSVC compilers, it’s ‘cdb.exe’ (without the quotes))

Solutions:
View compiler configuration:
setting -> compiler -> Toochain executables
Record the compiler installation path

Check out the Debuger Setting
setting -> dubuger -> default
Add Executable Path: C:\Program Files (x86)\CodeBlocks\MinGW\bin\ gDB32.exe (note: select GDB.exe for 32-bit system and gdb64.exe for 64-bit system)

Once you confirm the addition, debug is ready.

ARP – s in win7 (ARP binding under win7)

1. Want to use arp-s to add static binding MAC and IP address

2. OS Windows 7 English

3. (1) prompt The ARP entry addition failed: The requested operation requires elevation

solution, use the administrator identity to run cmd.exe

4.(2) arp-s, still error.

prompt: The ARP entry addition failed: Access is denied

reason: win 7 does not support binding to an existing arp address, that is, the ip-mac you want to add already exists in the arp table.

5. In win7 to achieve the binding according to the following operation

(1) using netsh I I show in

C:/Windows/system32 & gt; netsh i i show in

(2) view the idx

of the network card being used

independence Idx Met MTU State Name
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
1 50 4294967295 connected Loopback The Pseudo – Interface 1
13 25 1500 connected Wireless Network Connection
11 May 1500 disconnected Local Area Connection

(2) use the command (where 13 is the idx of the network card being used)

note the space bar, each symbol is followed by a space

C:/Windows/system32> Nesh-c “I I” add neighbors 13 “192.168.0.1” “00-22-b0-f8-0
d-6e”

(3) using arp-a

to see if

is successful

C:/Windows/system32 & gt; arp -a

Interface: 192.168.0.99-0 xd
Internet Address Physical Address Type
192.168.0.1 00-22 – b0 – f8-0-6 d e static

type static


6. Use netsh I I reset

is cancelled. Restart is required to take effect

launch failed.Binary Not found in Linux / Ubuntu solution

launch failed.Binary not found the solution:

first of all when you’re confused about mingw solutions online, I’ll tell you to stop reading about mingw. Under Linux, mingw is not used, but

is directly completed by Linux GCC

1, look at your Eclipse Console output, if there is g++ not found, it means your system g++ did not install successfully.

Solution: Ctrl+Alt+t to terminal (console) mode, enter sudo apt-get install g++

2, not yet?

Project-> Properties-> C/C++Build-> Settings-> Binary Parsers

check GNU Elf Parser and Elf Parser

and then don’t forget about Project-> Build All (Ctrl+B), then run (Ctrl+ F11)

. If you see the item below come out called Binaries, Congradulations! Configured successfully, ready to run!

3, not yet?

check your file name is XXX. CPP ?And XXX must not contain “. “such symbols


under Windows is best used mingw…

http://hi.baidu.com/doctorjohnson/blog/item/2fafa2431187e11d9213c67e.html

To solve the running error of eclipse for C / C + +: launch failed binary not found

sometimes we don’t like to use vs. Dear friends, like to use the eclipse practice c/c + + code, the construction of the environment, portal: http://jingyan.baidu.com/article/17bd8e523d7b9185ab2bb8f2.html

and then you might follow all the above steps and still hang when you run, and then you need to check the following two places:

1. If you haven’t built the project, click on the following position to build it and run it again. If not, scroll down to see

2. Change one setting :