0
#include <iostream>
#include <stdexcept>

#include <gtest\gtest.h>

class Vector {
private:
    int size;
    double* data;

public:
    // Constructor
    Vector() : size(0), data(nullptr) {}
    Vector(int s) : size(s), data(new double[s])
    {
        //std::cout << "Hello, Vector!";
    }
    ~Vector() {
        delete[] data;
    }
    // Copy constructor
    Vector(const Vector& other) : size(other.size), data(new double[other.size]) {
        for (int i = 0; i < size; i++) {
            data[i] = other.data[i];
        }
    }

    // Copy assignment operator
    Vector& operator=(const Vector& other) {
        if (this != &other) {
            delete[] data;
            size = other.size;
            data = new double[size];
            for (int i = 0; i < size; i++) {
                data[i] = other.data[i];
            }
        }
        return *this;
    }

    // Accessor methods
    int getSize() const {
        return size;
    }

    double& operator[](int i) {
        return data[i];
    }    
};

class Matrix {
private:
    int rows;
    int cols;
    Vector* data;

public:
    // Constructor
    Matrix() : rows(0), cols(0), data(nullptr) {}

    // Constructor
    Matrix(int rows_, int cols_) : rows(rows_), cols(cols_), data(new Vector[rows_]) {
        for (int i = 0; i < rows_; i++) {
            data[i] = Vector(cols_);
        }
    }

    // Destructor
    ~Matrix() {
        delete[] data;
    }

    // Copy constructor
    Matrix(const Matrix& other) : rows(other.rows), cols(other.cols), data(new Vector[other.rows]) {
        for (int i = 0; i < rows; i++) {
            data[i] = other.data[i];
        }
    }

    // Copy assignment operator
    Matrix& operator=(const Matrix& other) {
        if (this != &other) {
            delete[] data;
            rows = other.rows;
            cols = other.cols;
            data = new Vector[rows];
            for (int i = 0; i < rows; i++) {
                data[i] = other.data[i];
            }
        }
        return *this;
    }

    // Accessor methods
    int getRows() const {
        return rows;
    }

    int getCols() const {
        return cols;
    }

    double &operator()(int i, int j) {
        return data[i][j];
    }

    Matrix transpose() const {
        Matrix result(cols, rows);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                result(j, i) = data[i][j];
            }
        }
        return result;
    }

    void print(){
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                std::cout << data[i][j] << " ";
            }
            std::cout << std::endl;
        }
    }
};

int main(){
  Matrix m(2, 3);
    m(0,0) = 1;     m(0,1) = 2;     m(0,2) = 3;
    m(1,0) = 4;     m(1,1) = 5;     m(1,2) = 6;

    m.print();

    Matrix result = m.transpose();
    result.print();

  if(result(0,1) != 4){
    std::cout<<"Indexing is not working";
    std::cout<<std::endl;
  }
}

the following is my .replit file:

language = "cpp"

# Add this line to install the gtest package
deps = ["gtest"]

compile = "make -s"
run = "./main"
entrypoint = "main.cpp"
hidden = ["main", "**/*.o", "**/*.d", ".ccls-cache", "Makefile"]

[nix]
channel = "stable-22_11"

[gitHubImport]
requiredFiles = [".replit", "replit.nix", ".ccls-cache"]

[debugger]
support = true

[debugger.compile]
command = ["make", "main-debug"]
noFileArgs = true

[debugger.interactive]
transport = "stdio"
startCommand = ["dap-cpp"]

[debugger.interactive.initializeMessage]
command = "initialize"
type = "request"

[debugger.interactive.initializeMessage.arguments]
adapterID = "cppdbg"
clientID = "replit"
clientName = "replit.com"
columnsStartAt1 = true
linesStartAt1 = true
locale = "en-us"
pathFormat = "path"
supportsInvalidatedEvent = true
supportsProgressReporting = true
supportsRunInTerminalRequest = true
supportsVariablePaging = true
supportsVariableType = true

[debugger.interactive.launchMessage]
command = "launch"
type = "request"

[debugger.interactive.launchMessage.arguments]
MIMode = "gdb"
arg = []
cwd = "."
environment = []
externalConsole = false
logging = {}
miDebuggerPath = "gdb"
name = "g++ - Build and debug active file"
program = "./main-debug"
request = "launch"
setupCommands = [
    { description = "Enable pretty-printing for gdb", ignoreFailures = true, text = "-enable-pretty-printing" }
]
stopAtEntry = false
type = "cppdbg"

[languages]

[languages.cpp]
pattern = "**/*.{cpp,h}"

[languages.cpp.languageServer]
start = "ccls"

I ran the following command:

repl run repl.it/install-deps

The following is the output when I try to use gtest in my source file:

sh -c make -s
./main.cpp:3:10: fatal error: 'gtest\gtest.h' file not found
#include <gtest\gtest.h>
         ^~~~~~~~~~~~~~~
1 error generated.
make: *** [Makefile:10: main] Error 1
exit status 2

How can I use google-test in Replit?

user366312
  • 16,949
  • 65
  • 235
  • 452

0 Answers0