简体   繁体   中英

Netbeans: C++ Successful Build, Run Failed

UPDATE / SOLUTION: It seems this is a problem with Netbeans and not my code. I switched form NetBeans to CodeBlocks and use another Compiler and it works.

I'm trying to write my own Vector class in c++ at the moment. For this I am still using the built in vector, but I want to define my own operators. My problem is that building my project works ( "Build successful" ), but when I try to run my project it just says "Run failed" in the console. I have no idea what might be causing this error and the internet has not provided a solution yet. I'm using NetBeans Apache on Windows 10. Here is my code:

Vector.hh

#ifndef VECTOR_HH
#define VECTOR_HH

#include <vector>

using namespace std;

class Vector{
public:
    Vector();
    ~Vector();

    Vector(const Vector&);
    Vector(int d, double v);
    Vector(int d);

    Vector& operator=(const Vector&);
    double& operator[](const int);
    double operator[](const int) const;

    Vector& operator+(const Vector&);
    Vector& operator-(const Vector&);
    Vector& operator*(const double);

    double operator*(const Vector&);

    double norm();
    friend Vector& operator*(const double, Vector& v);

private:
    int dimension;
    vector<double>* values;
};

#endif /* VECTOR_HH */

Vector.cc

#include "Vector.hh"
#include <math.h>
#include <vector>

using namespace std;

Vector::Vector():dimension(0){
    values = new vector<double>();
}

Vector::~Vector(){
    dimension = 0;
    delete values;
}

Vector::Vector(const Vector& v):dimension(v.dimension){
    values = new vector<double>();
    for(int i = 0; i < dimension; i++){
        values->push_back(v[i]);
    }
}

Vector::Vector(int d, double v):dimension(d){
    values = new vector<double>();
    values->assign(dimension, v);
}

Vector::Vector(int d):dimension(d){
    values = new vector<double>();
    values->assign(dimension, 0);
}

Vector& Vector::operator=(const Vector& v){
    dimension = v.dimension;
    delete values;
    values = new vector<double>();
    for(int i = 0; i < dimension; i++){
        values->push_back(v[i]);
    }
    return *this;
}

Vector& Vector::operator*(double s){
    Vector* v = new Vector(dimension);
    for(int i = 0; i < dimension; i++){
        (*v)[i] = s * values->at(i);
    }
    return *v;
}

double& Vector::operator[](const int i){
    return values->at(i);
}

double Vector::operator[](const int i) const{
    return values->at(i);
}

Vector& Vector::operator+(const Vector& v){
    Vector* u = new Vector(dimension);
    for(int i = 0; i < dimension; i++){
        (*u)[i] = v[i] + values->at(i);
    }
    return *u;
}

Vector& Vector::operator-(const Vector& v){
    Vector* u = new Vector(dimension);
    for(int i = 0; i < dimension; i++){
        (*u)[i] = v[i] - values->at(i);
    }
    return *u;
}

double Vector::operator*(const Vector& v){
    double d = 0;
    if(v.dimension != dimension) return 0;
    for(int i = 0; i < dimension; i++){
        d = d + v[i] * values->at(i);
    }
    return d;
}

double Vector::norm(){
    double d = (*this) * (*this);
    return sqrt(d);
}

Vector& operator*(const double s, Vector& v){
    return v * s;
}

Main.cpp

#include <cstdlib>
#include "Vector.hh"
#include <iostream>

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    std::cout << "Hello there";
    Vector v(2);
    v[0] = 3;
    v[1] = -4;

    Vector u(2);

    return 0;
}

Thank you for your help :)

我无法使用 NetBeans 解决问题,但切换到另一个 IDE 和编译器(在我的例子中是 CodeBlocks)有所帮助,现在代码可以无缝运行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM