English 中文(简体)
console app c++
原标题:
  • 时间:2009-11-19 02:12:37
  •  标签:
  • c++
  • console

i m new to console apps and would appreciate some pointers...

i have created a new console app and (not finished but it should be working), i selected win32 console app and then selected empty project

here s my code:

#include <iostream>

void main() {

struct dude { 
    string name; 
    int age; 
} about; 

about.name = "jason"; 
about.age = 4000; 
cout << about.name << " " << about.age << endl;
}

The following errors i get are:

------ Build started: Project: Test, Configuration: Debug Win32 ------
Compiling...
codey.cpp
.codey.cpp(6) : error C2146: syntax error : missing  ;  before identifier  name 
.codey.cpp(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
.codey.cpp(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
.codey.cpp(10) : error C2039:  name  : is not a member of  main::dude 
        .codey.cpp(5) : see declaration of  main::dude 
.codey.cpp(12) : error C2065:  cout  : undeclared identifier
.codey.cpp(12) : error C2039:  name  : is not a member of  main::dude 
        .codey.cpp(5) : see declaration of  main::dude 
.codey.cpp(12) : error C2065:  endl  : undeclared identifier
Build log was saved at "file://c:UsersJasonDocumentsVisual Studio 2008ProjectsTestTestDebugBuildLog.htm"
Test - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

can somebody please tell me how i can get this to debug? what have i done wrong here?


Working...
#include <iostream> 
#include <string> 

struct Dude { 
    std::string name; 
    int age; };

    int main(int i) 
    { 
        while(i<4000)
        {
            i++;
        using namespace std; 
        Dude jason = { "Jason", i }; 
        cout << jason.name << " is " << jason.age << " years old.
"; 
        }

        return 0; 
    }

thank you all for your help :D

最佳回答
#include <iostream>
#include <string>

struct Dude {
  std::string name;
  int age;
};

int main() {
  using namespace std;
  Dude jason = { "Jason", 4000 };
  cout << jason.name << " is " << jason.age << " years old.
";
  return 0;
}

In no particular order:

  • define your types outside of functions
  • name types consistently ("Dude" here)
  • provide values when initializing objects
    • I ve used aggregate initialization here, you will use slightly different syntax when you write a constructor (ctor)
  • the C++ stdlib is almost exclusively in the std namespace
    • you can place using namespace std; at function scope, if you like, instead of typing std:: within that function body
  • the C++ stdlib string type is from the <string> header
  • main returns int

Fixing these problems isn t really debugging, you just need to learn the correct syntax. Make sure you have a good book (e.g. Accelerated C++ by Koenig and Moo) and a good teacher doesn t hurt.

问题回答

The correct code should look like:

#include <iostream>
#include <string> 
using namespace std;

struct dude {
  string name;
  int age;
};

int main() { 

  struct dude about;
  about.name = "jason";
  about.age = 4000;

  cout << about.name << " " << about.age << endl;
  return 0;
}

EDIT: Added the necessary includes so that it compiles. Also, as a best practise, moved type definition outside of function.

In addition to Joy s answer, you need to use the std namespace:

std::cout << about.name << " " << about.age << endl;

Also, what are you trying to achieve? Maybe it s better if dude were a class?

You mean how to get it to compile!

You forgot to put

using namespace std;

I think you should pick up a good C++ book, spend some time reading it and come to SO like site afterwards. Just a sincere advise, no offense.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签