Gold Member
- 4,662
- 372
count and endl belong to the std namespace. To use them in your program, you should prefix each use of these with "std::". In other words, as std::count or std::endl. Another option, but not recommended, is to add using std; above the header for your main function.MathematicalPhysicist said:C++:#include <iostream> #define N 3 void main() { int i; int *u, *v, w; u = new int[N + 1]; u[1] = 4; u[2] = -5; u[3] = 3; count << endl << "Vector u:" << endl; for (i = 1; i <= N; i++) count << u[i] << " "; count << endl; v = new int[N + 1]; v[1] = -2; v[2] = 3; v[3] = 7; count << "Vector v:" << endl; for (i = 1; i <= N; i++) count << v[i] << " "; count << endl; w = 0; for (i = 1; i <= N; i++) w += u[i] * v[i]; count << "the product w=u.v is " << w << endl; delete u, v; }
What you described is backwards: prototypes (what you have above) should appear before the operator definitions, not after them.MathematicalPhysicist said:Hi I have another question, in the last code above that I copied, is it ok first to declare the operators or do I need first to define these operators?
I first defined them, so the next code is copied after I finished defining these operators:
C++:const vectors operator+=(const vectors); const vectors operator-=(const vectors); const vectors operator*=(const T&); const vectors operator/=(const T&); };
When you start the program by clicking the exe's icon, your program opens a command window, runs, and then closes that window. There are a couple of things you can do to keep that window open:MathematicalPhysicist said:@Mark44 thanks.
Btw, do you happen to know why when I am executing a source file in visual C++ 2015 community the window of the .exe file appears and disappears without me pressing any key to exit from the programme?
The Debug gives me the following lines:
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. Cannot find or open the PDB file.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Cannot find or open the PDB file.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Cannot find or open the PDB file.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Symbols loaded.
The program '[6836] Project6.exe' has exited with code 0 (0x0).
I picked an empty project file and as a source file a cpp file, what am I do wrong here that the window exits automatically without me pressing any key?
Drives me nuts...
@Mark44 I get an error "'cin' undeclared identifier" on line 25.Mark44 said:When you start the program by clicking the exe's icon, your program opens a command window, runs, and then closes that window. There are a couple of things you can do to keep that window open:
1. Open a separate command window, and CD (change directory) to the directory that contains your executable. Then type the name of the executable, which starts to run in the command window you have opened.
2. Add in input statement at the end of your main() function, such as cin >> ch; (You need to also declare ch as type char.) The command window will stay open until you type a character, and will then close.
The lines you show above aren't produced by the debug window, I don't believe. They are produced by the Output window. You can ignore all of them.
#include <iostream>
#define N 3
void main()
{
char ch;
int i;
int *u, *v, w;
u = new int[N + 1];
u[1] = 4; u[2] = -5; u[3] = 3;
std::count << std::endl << "Vector u:" << std::endl;
for (i = 1; i <= N; i++)
std::count << u[i] << " ";
std::count << std::endl;
v = new int[N + 1];
v[1] = -2; v[2] = 3; v[3] = 7;
std::count << "Vector v:" << std::endl;
for (i = 1; i <= N; i++)
std::count << v[i] << " ";
std::count << std::endl;
w = 0;
for (i = 1; i <= N; i++)
w += u[i] * v[i];
std::count << "the product w=u.v is " << w << std::endl;
delete u, v;
cin >> ch;
}
Open a command window:MathematicalPhysicist said:BTW @Mark44 what exactly should I do in your first option you've given me? (I remembered how to do it in java but forgotten).
Yes, I neglected to add "std::" in what I wrote. Thanks for correcting me.MisterX said:Replacing "cin" with "std::cin" should fix that.
MathematicalPhysicist said:So you tell me to ditch this book, and the other book. any other recommendations on books, perhaps "numerical recipes"?
@wle I want to teach myself using C++ to write programmes to solve numerically PDEs and integral equations for my research, this is why I used such books with those titles.wle said:The best advice to give you depends on why you're doing this and what you're ultimately hoping to accomplish. You haven't explained that yet.
So far, this thread seems to consist of you:
I'd really like to see an explanation here. What are you trying to accomplish that you think this pattern is the best way to go about it?
- Copying hundreds of lines of C++ code seemingly at random from books (of questionable quality, at that).
- Asking others to fix even the most basic syntax errors for you when it doesn't compile.
- Asking all your questions here, even those that a simple Google search could answer much faster.
I don't have dev C++ so can't speak to its behavior. If you run your program from Windows (and possibly from Linux), the OS opens a command window and your program starts running. When the program is finished, it returns to the caller (Windows or Linux) and closes the command window.MathematicalPhysicist said:@Mark44 why does the programme exits the command line so quickly? in dev C++ it didn't do that.
It did, it's just doing something else that you aren't aware of that I'm surprised hasn't been mentioned.MathematicalPhysicist said:@Mark44 why does the programme exits the command line so quickly? in dev C++ it didn't do that.
MathematicalPhysicist said:@wle I want to teach myself using C++ to write programmes to solve numerically PDEs and integral equations for my research, this is why I used such books with those titles.
Do you have a recommendation?