2011-03-10 67 views
-2

project1.cppC++ OOP编程

#include "stdafx.h" 
#include "Bicycle.cpp" 
using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    bool runP = true; 
    do { 
    Bicycle object(); 
    char oType; 
    cout << "Would you like a (B)icycle, or (A)nimal? E for Exit\n"; 
    cin >> oType; 

    if (oType == 'B' || oType == 'b') { 
     int seat, wheels; 
     string brand; 
     cout << "How many wheels does the bike have?\n"; 
     cin >> wheels; 
     object().setWheels(wheels); 
     cout << "How many seats does the bike have?\n"; 
     cin >> seat; 
     object().setSeats(seat); 
     cout << "What is the brand of the bike?\n"; 
     cin >> brand; 
     object().setBrand(brand); 
     object().toString(); 
    } else if (oType == 'A' || oType == 'a') { 
    } else if (oType == 'e' || oType == 'E') { 
     runP = false; 
    } 

    } while (runP == true); 
    return 0; 
} 

Bicycle.h

#include <string> 

class Bicycle { 

private: 
    int wheels; 
    int seats; 
    std::string brand; 
public: 
    Bicycle(); 
    Bicycle(int w, int s, std::string b); 
    int getWheels(); 
    int getSeats(); 
    std::string getBrand(); 
    void setWheels(int w); 
    void setSeats(int s); 
    void setBrand(std::string b); 
    void toString(); 

}; 

Bicycle.cpp

#include "stdafx.h" 
#include "Bicycle.h" 
#include <string> 


    Bicycle::Bicycle() { 
    } 
    Bicycle::Bicycle(int w, int s, std::string b) { 
    } 
    int Bicycle::getWheels() { 
     return wheels; 
    } 
    int Bicycle::getSeats() { 
     return seats; 
    } 
    std::string Bicycle::getBrand() { 
     return brand; 
    } 
    void Bicycle::setWheels(int w) { 
     wheels = w; 
    } 
    void Bicycle::setSeats(int s) { 
     seats = s; 
    } 
    void Bicycle::setBrand(std::string b) { 
     brand = b; 
    } 
    void Bicycle::toString(){ 
     std::cout << "Bike object has: Brand: " << getBrand() << ", wheels: " << getWheels() << ", seats: " << getSeats() << "\n"; 
    } 

stdaf x.h

#pragma once 

#include "targetver.h" 
#include <stdio.h> 
#include <tchar.h> 
#include <iostream> 

错误:

1>------ Build started: Project: project1, Configuration: Debug Win32 ------ 
1> stdafx.cpp 
1> Bicycle.cpp 
1> Generating Code... 
1> Compiling... 
1> project1.cpp 
1> Generating Code... 
1> Skipping... (no relevant changes detected) 
1> Window.cpp 
1> Animal.cpp 
1>project1.obj : error LNK2005: "public: __thiscall Bicycle::Bicycle(void)" ([email protected]@[email protected]) already defined in Bicycle.obj 
1>project1.obj : error LNK2005: "public: __thiscall Bicycle::Bicycle(int,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" ([email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) already defined in Bicycle.obj 
1>project1.obj : error LNK2005: "public: int __thiscall Bicycle::getWheels(void)" ([email protected]@@QAEHXZ) already defined in Bicycle.obj 
1>project1.obj : error LNK2005: "public: int __thiscall Bicycle::getSeats(void)" ([email protected]@@QAEHXZ) already defined in Bicycle.obj 
1>project1.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Bicycle::getBrand(void)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@XZ) already defined in Bicycle.obj 
1>project1.obj : error LNK2005: "public: void __thiscall Bicycle::setWheels(int)" ([email protected]@@[email protected]) already defined in Bicycle.obj 
1>project1.obj : error LNK2005: "public: void __thiscall Bicycle::setSeats(int)" ([email protected]@@[email protected]) already defined in Bicycle.obj 
1>project1.obj : error LNK2005: "public: void __thiscall Bicycle::setBrand(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) already defined in Bicycle.obj 
1>project1.obj : error LNK2005: "public: void __thiscall Bicycle::toString(void)" ([email protected]@@QAEXXZ) already defined in Bicycle.obj 
1>project1.obj : error LNK2019: unresolved external symbol "class Bicycle __cdecl object(void)" ([email protected]@[email protected]@XZ) referenced in function _wmain 
1>C:\Users\Hash\Documents\Visual Studio 2010\Projects\project1\Debug\project1.exe : fatal error LNK1120: 1 unresolved externals 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+6

你的问题在哪里? – 2011-03-10 23:09:41

+0

通常情况下,人们表示他们会在解决问题的过程中付出一些努力,列出他们所尝试的内容。否则,您可能会遇到假设,您并未尝试解决任何问题,只是将您的代码/错误复制粘贴到Web表单中并点击提交。 – 2011-03-10 23:14:35

+1

想知道 - setWheels如何为真正的自行车工作? – 2011-03-10 23:43:57

回答

4

相当肯定的,而不是

#include "Bicycle.cpp" 

你想

#include "Bicycle.h" 

随着第一,一切都在Bicycle.cpp得到复制粘贴到project1.cpp,你会得到一个组的自行车功能定义重复定义当编译project1.cpp时,另一个是编译Bicycle.cpp时。

5

project1.cpp应该

#include "bicycle.h" 

#include "bicycle.cpp"