Visual Studio 2012声明错误?

时间:2014-04-13 06:28:30

标签: visual-c++ visual-studio-2012

当我尝试在Visual Studio 2012中构建项目时,我遇到了问题。 这是我的代码:

**

Vector.h
--------

**

#pragma once
class Vector {
private:
    static const int DEF_SIZE = 10;
    size_t size;
    int *elems;
public:
    Vector() : Vector(DEF_SIZE, nullptr, 0){};
    Vector(size_t max_size) :Vector(max_size, nullptr, 0){};
    Vector(size_t max_size, const int* data, size_t data_size);
    Vector(const Vector& v);
    virtual Vector& Vector::operator=(const Vector& v);
    virtual ~Vector();
//  int& Vector::operator[](size_t index);
    virtual void print(void) const;
    size_t get_size() const;
    int get_elem(size_t pos) const;
    virtual void set_elem(size_t pos, const int new_value);

};

**`Vector.cpp`**

#include "Vector.h"
#include <iostream>
/*
Vector::Vector(){
this->size = DEF_SIZE;
this->elems = new int[this->size];
for (size_t i = 0; i < this->size; i++)
*(this->elems + i) = 0;
}

Vector::Vector(size_t max_size) {
this->size = (max_size >= 1)
? max_size : Vector::DEF_SIZE;
this->elems = new int[this->size];
for (size_t i = 0; i < this->size; i++)
*(this->elems + i) = 0;
}
*/

Vector::Vector(size_t max_size, const int* data, size_t data_size) {
//  std::cout << "Vector Constructor" << std::endl;
    if (max_size < data_size)
        max_size = data_size;
    this->size = (max_size >= 1) ? max_size : Vector::DEF_SIZE;
    this->elems = new int[this->size];
    for (size_t i = 0; i < this->size; i++)
        *(this->elems + i) = (i < data_size) ? data[i] : 0;
}

Vector::~Vector(){
//  std::cout << "Vector Destructor" << std::endl;
    delete[] this->elems;
}

Vector::Vector(const Vector& v){
//  std::cout << "Vector Constructor" << std::endl; 
    this->size = v.get_size();enter code here
    this->elems = new int[this->size];
    for (size_t i = 0; i<this->size; i++)
        *(this->elems + i) = v.get_elem(i + 1);
}

Vector& Vector::operator=(const Vector& v){
    if (this != &v){
        this->size = v.get_size();
        delete[] this->elems;
        this->elems = new int[this->size];
        for (size_t i = 0; i<this->size; i++)
            *(this->elems + i) = v.get_elem(i + 1);
    }
    return *this;
}

void Vector::print(void) const{
    for (size_t i = 0; i < this->size; i++)
        std::cout << *(this->elems + i) << " ";
    std::cout << std::endl;
}


size_t Vector::get_size() const{
    return this->size;
}


int Vector::get_elem(size_t pos) const{
    return *(this->elems + pos - 1);
}


void Vector::set_elem(size_t pos, const int new_value) {
    if (pos >= 1 && pos <= this->size)
        *(this->elems + pos - 1) = new_value;
}


**Exvector.h**

#pragma once
#include "Vector.h"
#include <iostream>

class ExtVector : public Vector{
    static const size_t DEF_SIZE;
    size_t maxVal=0;
    virtual void swap(int elem1, int elem2);
public:
    ExtVector() :ExtVector(DEF_SIZE,nullptr,0){};
    ExtVector(size_t length) :ExtVector(length, nullptr, 0){};
    ExtVector(size_t length, const int* data, size_t data_size) : Vector(length, data, data_size){ this->calculate();
                                                                                                //  std::cout << "ExtVector Constructor" << std::endl;
                                                                                                };


    ExtVector(const ExtVector& v) :Vector(v){ this->calculate();
                                                // std::cout << "ExtVector Constructor" << std::endl;
                                            };


    ExtVector(const Vector& v) :Vector(v){ this->calculate();   
                                            ///std::cout << "ExtVector Constructor" << std::endl;
                                            };


    virtual ~ExtVector();
    void calculate();
    virtual void sort();
    size_t get_max_index() const;
    void print(void) const override{
        std::cout << "Index of max value" << this->maxVal << std::endl;
        Vector::print();
    };
    void set_elem(size_t pos, const int new_value) override;
    virtual ExtVector& operator=(const Vector& v);
    virtual ExtVector& operator=(const ExtVector& v);
};

**Exvector.cpp**

#include "Extvector.h"
#include <iostream>

ExtVector::~ExtVector(){
    Vector::~Vector();
//  std::cout << "ExtVector DesConstructor" << std::endl;
}

void ExtVector::calculate(){
    size_t maxInd = 0;
    for (size_t i = 1; i < this->get_size(); i++){
        if (this->get_elem(i+1)>this->get_elem(maxInd+1))
            maxInd = i;
    }
    this->maxVal = maxInd;
}
ExtVector& ExtVector::operator=(const Vector& v){
    Vector::operator=(v);
    this->calculate();
    return *this;
}

ExtVector& ExtVector::operator=(const ExtVector& v){
    Vector::operator=(v);
    this->calculate();
    return *this;
}

size_t ExtVector::get_max_index() const{
    return maxVal;
}

void ExtVector::set_elem(size_t pos,const int new_value){
    Vector::set_elem(pos, new_value);
    this->calculate();
}



void ExtVector::swap(int elem1, int elem2){
    int temp = this->get_elem(elem1 - 1);
    this->set_elem(elem1 - 1, this->get_elem(elem2 - 1));
    this->set_elem(elem2 - 1, temp);
}



void ExtVector::sort(){
    int max;
    for (size_t i = 0; i < this->get_size()-1;i++)
        for (size_t j = i + 1; j < this->get_size();j++)
        if (this->get_elem(i + 1) < this->get_elem(j + 1))
            this->swap(i, j);

    this->calculate();
}

当我尝试构建此项目时,我收到了以下错误:

1>------ Build started: Project: Task3, Configuration: Debug Win32 ------
1>  Vector.cpp
1>c:\users\samuel\documents\visual studio 2012\projects\oop\task3\vector.h(8): error C2614: 'Vector' : illegal member initialization: 'Vector' is not a base or member
1>c:\users\samuel\documents\visual studio 2012\projects\oop\task3\vector.h(9): error C2614: 'Vector' : illegal member initialization: 'Vector' is not a base or member
1>  Exvector.cpp
1>c:\users\samuel\documents\visual studio 2012\projects\oop\task3\exvector.cpp(1): fatal error C1083: Cannot open include file: 'Extvector.h': No such file or directory
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

如果有人能给我一点指导,我将不胜感激。谢谢!

0 个答案:

没有答案