C/C++ A programme that doesn't get compiled in C++

  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
The discussion revolves around a C++ code snippet for polynomial operations that fails to compile due to multiple errors. Key issues include missing implementations for certain functions and operators, improper use of the tilde character in destructor definitions, and the need for the correct inclusion of libraries for functions like max and min. Participants suggest cleaning up the code formatting and ensuring all necessary components from the source material are included. The conversation highlights the importance of careful attention to syntax and structure in C++ programming to avoid compilation errors. The code requires further refinement to resolve the identified issues and achieve successful compilation.
  • #31
@D H where exactly am I missing a close brace and a semicolon?

So you tell me to ditch this book, and the other book. any other recommendations on books, perhaps "numerical recipes"?
 
Technology news on Phys.org
  • #32
@D H how do I add those libraries to visual C++ STL library?
 
  • #33
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;
    cout << endl << "Vector u:" << endl;
    for (i = 1; i <= N; i++)
        cout << u[i] << " ";
    cout << endl;
    v = new int[N + 1];
    v[1] = -2; v[2] = 3; v[3] = 7;
    cout << "Vector v:" << endl;
    for (i = 1; i <= N; i++)
        cout << v[i] << " ";
    cout << endl;
    w = 0;
    for (i = 1; i <= N; i++)
        w += u[i] * v[i];
    cout << "the product w=u.v is " << w << endl;
    delete u, v;
}
cout 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::cout or std::endl. Another option, but not recommended, is to add using std; above the header for your main function.

cout is one of the standard streams that your program can use for input or output (cout, cin, cerr, clog). The "out" part of the name indicates that this is an output stream. The other standard streams are for input, errors, and logging. The statement cout << v[1]; sends the value of v[1] to the standard output stream (typically, the screen). endl is the newline character.
 
Last edited:
  • #34
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&);
};
What you described is backwards: prototypes (what you have above) should appear before the operator definitions, not after them.
 
  • #35
@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...
 
  • #36
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...
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.

Edit: the above should be std::cin >> ch;

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.
 
Last edited:
  • #37
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.
@Mark44 I get an error "'cin' undeclared identifier" on line 25.
C++:
#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::cout << std::endl << "Vector u:" << std::endl;
    for (i = 1; i <= N; i++)
        std::cout << u[i] << " ";
    std::cout << std::endl;
    v = new int[N + 1];
    v[1] = -2; v[2] = 3; v[3] = 7;
    std::cout << "Vector v:" << std::endl;
    for (i = 1; i <= N; i++)
        std::cout << v[i] << " ";
    std::cout << std::endl;
    w = 0;
    for (i = 1; i <= N; i++)
        w += u[i] * v[i];
    std::cout << "the product w=u.v is " << w << std::endl;
    delete u, v;
    cin >> ch;
}
 
  • #38
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).
 
  • #39
MathematicalPhysicist said:
@Mark44 I get an error "'cin' undeclared identifier" on line 25.
Replacing "cin" with "std::cin" should fix that.
 
  • #40
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).
Open a command window:
Start --> Run, then type cmd
When the command window opens, change directories (CD) to the directory that contains your executable (the .exe file).
Type the name of the exe (you don't need to add the .exe file extension, and then hit Enter.

MisterX said:
Replacing "cin" with "std::cin" should fix that.
Yes, I neglected to add "std::" in what I wrote. Thanks for correcting me.
 
  • #41
MathematicalPhysicist said:
So you tell me to ditch this book, and the other book. any other recommendations on books, perhaps "numerical recipes"?

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:
  1. Copying hundreds of lines of C++ code seemingly at random from books (of questionable quality, at that).
  2. Asking others to fix even the most basic syntax errors for you when it doesn't compile.
  3. Asking all your questions here, even those that a simple Google search could answer much faster.
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?
 
Last edited:
  • Like
Likes D H and Mark44
  • #42
@Mark44 why does the programme exits the command line so quickly? in dev C++ it didn't do that.
 
  • #43
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:
  1. Copying hundreds of lines of C++ code seemingly at random from books (of questionable quality, at that).
  2. Asking others to fix even the most basic syntax errors for you when it doesn't compile.
  3. Asking all your questions here, even those that a simple Google search could answer much faster.
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?
@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?
 
  • #44
MathematicalPhysicist said:
@Mark44 why does the programme exits the command line so quickly? in dev C++ it didn't do that.
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.
 
  • #45
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.

Ok, so when your program gets run, it starts in the main() function, and it runs through line by line until it gets the the end. It then IMMEDIATELY gets completed and removed by the OS.

Dev-C++ is doing something different. It's not meant to run your program, it's meant to help you create it. When you tell Dev-C++ to run your code, what you are actually telling it to do is to DEBUG your program. It wraps your program in a gbd process, and that process is the one that stays open in Dev-C++.

I had the same question about ten years ago. :) BTW, Dev-C++ is very old and rotted. I recommend changing to Code::Blocks, it'll even import the Dev-C++ file and upgrade it for you.
 
Last edited:
  • #46
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?

You should concentrate on learning (at least) the basics of C++ first before trying to use it for numerical programming. (If you're asking questions like what cout is and where to put closing braces and std::s, you don't know the basics.) D H already pointed out some links to get you started with that.

It's reasonable that you'd want to copy existing code or use an available numerical library rather than waste time reinventing the wheel, but you'll still have to understand the features of the C++ language that such libraries use. For example, if a numerical library is provided in the form of a collection of template functions and classes, you're going to struggle to use it (at best) if you're not already familiar with C++ templates and classes. Chances are no library is going to do everything for you (otherwise it would be a complete working program and not a library), so you'll probably have to do some of your own programming anyway.

One specific recommendation I'll make: don't copy/paste code while you're learning C++ (or any other programming language or technique, for that matter). Learning C++ will probably involve copying and trying out code examples -- you should actually type these out yourself. This will slow you down enough that you can think about and better internalise what the code does as you type it, as well as get you used to the language's syntax.

NB: C++ is a very complicated language to learn and use and if you start learning it you shouldn't expect to immediately be able to use it for numerical work. If you're pressed for time you may be better off using something else (e.g. Fortran, Python, or even just Matlab) instead.
 

Similar threads

  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
89
Views
6K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K