2015-10-18 46 views
2

我是編程新手,我是一名開始學習C++的學生。我想通過一本關於C++的好書讓我領先我的課,編程原則&練習(第二版)被推薦給我。 我在第一個練習叫「你好,世界!」我因爲一些編譯錯誤而被卡住了。 (我正在使用Visual Studio 2015)。Bjarne Stroustrup的頭文件編譯錯誤(PPP2 - 編程原理與實踐)

當我嘗試運行該程序的輸出顯示了這個

1>------ Rebuild All started: Project: Hello,World!, Configuration: Debug Win32 ------ 
1> Hello,World!.cpp 
1>MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" ([email protected]@YAHXZ) 
1>F:\DAE v2015\1) Programming\C++ Projects\Hello,World!\Debug\Hello,World!.exe : fatal error LNK1120: 1 unresolved externals 
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ========== 

他的頭文件的代碼如下:

/* 
    std_lib_facilities.h 
*/ 

/* 
    simple "Programming: Principles and Practice using C++ (second edition)" course header to 
    be used for the first few weeks. 
    It provides the most common standard headers (in the global namespace) 
    and minimal exception/error support. 

    Students: please don't try to understand the details of headers just yet. 
    All will be explained. This header is primarily used so that you don't have 
    to understand every concept all at once. 

    By Chapter 10, you don't need this file and after Chapter 21, you'll understand it 

    Revised April 25, 2010: simple_error() added 

    Revised November 25 2013: remove support for pre-C++11 compilers, use C++11: <chrono> 
    Revised November 28 2013: add a few container algorithms 
    Revised June 8 2014: added #ifndef to workaround Microsoft C++11 weakness 
*/ 

#ifndef H112 
#define H112 251113L 


#include<iostream> 
#include<iomanip> 
#include<fstream> 
#include<sstream> 
#include<cmath> 
#include<cstdlib> 
#include<string> 
#include<list> 
#include <forward_list> 
#include<vector> 
#include<unordered_map> 
#include<algorithm> 
#include <array> 
#include <regex> 
#include<random> 
#include<stdexcept> 

//------------------------------------------------------------------------------ 


//------------------------------------------------------------------------------ 

typedef long Unicode; 

//------------------------------------------------------------------------------ 

using namespace std; 

template<class T> string to_string(const T& t) 
{ 
    ostringstream os; 
    os << t; 
    return os.str(); 
} 

struct Range_error : out_of_range { // enhanced vector range error reporting 
    int index; 
    Range_error(int i) :out_of_range("Range error: "+to_string(i)), index(i) { } 
}; 


// trivially range-checked vector (no iterator checking): 
template< class T> struct Vector : public std::vector<T> { 
    using size_type = typename std::vector<T>::size_type; 

#ifdef _MSC_VER 
    // microsoft doesn't yet support C++11 inheriting constructors 
    Vector() { } 
    explicit Vector(size_type n) :std::vector<T>(n) {} 
    Vector(size_type n, const T& v) :std::vector<T>(n,v) {} 
    template <class I> 
    Vector(I first, I last) : std::vector<T>(first, last) {} 
    Vector(initializer_list<T> list) : std::vector<T>(list) {} 
#else 
    using std::vector<T>::vector; // inheriting constructor 
#endif 

    T& operator[](unsigned int i) // rather than return at(i); 
    { 
     if (i<0||this->size()<=i) throw Range_error(i); 
     return std::vector<T>::operator[](i); 
    } 
    const T& operator[](unsigned int i) const 
    { 
     if (i<0||this->size()<=i) throw Range_error(i); 
     return std::vector<T>::operator[](i); 
    } 
}; 

// disgusting macro hack to get a range checked vector: 
#define vector Vector 

// trivially range-checked string (no iterator checking): 
struct String : std::string { 
    using size_type = std::string::size_type; 
// using string::string; 

    char& operator[](unsigned int i) // rather than return at(i); 
    { 
     if (i<0||size()<=i) throw Range_error(i); 
     return std::string::operator[](i); 
    } 

    const char& operator[](unsigned int i) const 
    { 
     if (i<0||size()<=i) throw Range_error(i); 
     return std::string::operator[](i); 
    } 
}; 


namespace std { 

    template<> struct hash<String> 
    { 
     size_t operator()(const String& s) const 
     { 
      return hash<std::string>()(s); 
     } 
    }; 

} // of namespace std 


struct Exit : runtime_error { 
    Exit(): runtime_error("Exit") {} 
}; 

// error() simply disguises throws: 
inline void error(const string& s) 
{ 
    throw runtime_error(s); 
} 

inline void error(const string& s, const string& s2) 
{ 
    error(s+s2); 
} 

inline void error(const string& s, int i) 
{ 
    ostringstream os; 
    os << s <<": " << i; 
    error(os.str()); 
} 


template<class T> char* as_bytes(T& i) // needed for binary I/O 
{ 
    void* addr = &i; // get the address of the first byte 
         // of memory used to store the object 
    return static_cast<char*>(addr); // treat that memory as bytes 
} 


inline void keep_window_open() 
{ 
    cin.clear(); 
    cout << "Please enter a character to exit\n"; 
    char ch; 
    cin >> ch; 
    return; 
} 

inline void keep_window_open(string s) 
{ 
    if (s=="") return; 
    cin.clear(); 
    cin.ignore(120,'\n'); 
    for (;;) { 
     cout << "Please enter " << s << " to exit\n"; 
     string ss; 
     while (cin >> ss && ss!=s) 
      cout << "Please enter " << s << " to exit\n"; 
     return; 
    } 
} 



// error function to be used (only) until error() is introduced in Chapter 5: 
inline void simple_error(string s) // write ``error: s and exit program 
{ 
    cerr << "error: " << s << '\n'; 
    keep_window_open();  // for some Windows environments 
    exit(1); 
} 

// make std::min() and std::max() accessible on systems with antisocial macros: 
#undef min 
#undef max 


// run-time checked narrowing cast (type conversion). See ???. 
template<class R, class A> R narrow_cast(const A& a) 
{ 
    R r = R(a); 
    if (A(r)!=a) error(string("info loss")); 
    return r; 
} 

// random number generators. See 24.7. 



inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); } 

inline int randint(int max) { return randint(0, max); } 

//inline double sqrt(int x) { return sqrt(double(x)); } // to match C++0x 

// container algorithms. See 21.9. 

template<typename C> 
using Value_type = typename C::value_type; 

template<typename C> 
using Iterator = typename C::iterator; 

template<typename C> 
    // requires Container<C>() 
void sort(C& c) 
{ 
    std::sort(c.begin(), c.end()); 
} 

template<typename C, typename Pred> 
// requires Container<C>() && Binary_Predicate<Value_type<C>>() 
void sort(C& c, Pred p) 
{ 
    std::sort(c.begin(), c.end(), p); 
} 

template<typename C, typename Val> 
    // requires Container<C>() && Equality_comparable<C,Val>() 
Iterator<C> find(C& c, Val v) 
{ 
    return std::find(c.begin(), c.end(), v); 
} 

template<typename C, typename Pred> 
// requires Container<C>() && Predicate<Pred,Value_type<C>>() 
Iterator<C> find_if(C& c, Pred p) 
{ 
    return std::find_if(c.begin(), c.end(), p); 
} 

#endif //H112 

預先感謝您的幫助! PS:如果你認爲我應該從另一本書開始,因爲我可能遇到更多像這樣的問題,請告訴我:)

+0

您能否顯示您的'Hello,World!.cpp'文件? – atkins

+0

這是我在Hello,World!.cpp文件中寫的唯一的東西: #include「F:\ DAE v2015 \ 1)Programming \ C++ Projects/std_lib_facilities.h」 – Feast

+0

在這種情況下,就像@Mykola所說:你需要定義一個'int main()'方法來使它成爲合法的C++程序。 – atkins

回答

1

將main.cpp文件添加到代碼中,如果它的第一個C++程序你寫它只能包含這個代碼。

#include <iostream> 
#include <conio.h> 

int main() { 

    std::cout << "Hello, World!" << std::endl; 
    _getch(); 
    return 0; 
} 

課程1)所有的C++程序都必須包含main()函數的實現。

本人來說,我開始了我在C++中的第一個步驟與圖書Teach Yourself C++ in 24 hours

在我看來,Bjarne的書是相當困難的閱讀C++新手

+1

請不要建議OP做那。這本書是關於一個完全不應該教的語言的現在完全舊版本。另外,我真的認爲在24小時內自學一門編程語言並不是一個好主意。 – einpoklum

+0

新版本的語言並不比舊版本更好,特別是新手更復雜。 – Mykola

+0

我去他的網站上有一個他的頭文件的更新版本(他的書鏈接是過期的),我的意思是另一本適合我的書是我不需要額外的修復代碼。如果我理解正確C++最近有升級,那麼肯定找到這樣一本書並不容易? – Feast

0

這就是發生在我刪除行#include "stdafx.h" 謝謝爲你的幫助傢伙:D抱歉,我很糟糕。

'Hello,World!.exe' (Win32): Loaded 'F:\DAE v2015\1) Programming\C++ Projects\Hello,World!\Debug\Hello,World!.exe'. Symbols loaded. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. Cannot find or open the PDB file. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Cannot find or open the PDB file. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Cannot find or open the PDB file. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file. 
'Hello,World!.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel.appcore.dll'. Cannot find or open the PDB file. 
The program '[892] Hello,World!.exe' has exited with code 0 (0x0). 
+0

就這樣完成了,祝你好運 – Mykola

+0

謝謝,但是爲什麼我的程序一啓動就退出? – Feast

+0

我會更新我的答案 – Mykola