программирование на c ++ - PullRequest
0 голосов
/ 11 марта 2011

project1.cpp

#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";
    }

stdafx.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)" (??0Bicycle@@QAE@XZ) 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> >)" (??0Bicycle@@QAE@HHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Bicycle.obj
1>project1.obj : error LNK2005: "public: int __thiscall Bicycle::getWheels(void)" (?getWheels@Bicycle@@QAEHXZ) already defined in Bicycle.obj
1>project1.obj : error LNK2005: "public: int __thiscall Bicycle::getSeats(void)" (?getSeats@Bicycle@@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)" (?getBrand@Bicycle@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Bicycle.obj
1>project1.obj : error LNK2005: "public: void __thiscall Bicycle::setWheels(int)" (?setWheels@Bicycle@@QAEXH@Z) already defined in Bicycle.obj
1>project1.obj : error LNK2005: "public: void __thiscall Bicycle::setSeats(int)" (?setSeats@Bicycle@@QAEXH@Z) 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> >)" (?setBrand@Bicycle@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Bicycle.obj
1>project1.obj : error LNK2005: "public: void __thiscall Bicycle::toString(void)" (?toString@Bicycle@@QAEXXZ) already defined in Bicycle.obj
1>project1.obj : error LNK2019: unresolved external symbol "class Bicycle __cdecl object(void)" (?object@@YA?AVBicycle@@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 ==========

Ответы [ 2 ]

5 голосов
/ 11 марта 2011

project1.cpp следует

 #include "bicycle.h" 

не

 #include "bicycle.cpp"
4 голосов
/ 11 марта 2011

Уверен, вместо

#include "Bicycle.cpp"

вы хотите

#include "Bicycle.h"

С первым все в Bicycle.cpp копируется и копируется в project1.cpp, и вы получаете дубликаты определений содин набор функций велосипеда определен при компиляции project1.cpp, а другой - при компиляции Bicycle.cpp.

...