0

I am new to programming how can I solve this error could someone help me? tried it by myself but wasn't able to! I tried changing the variable data types but got other errors. Please suggest me what can I do to solve this error.

#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <vector>
#include <complex>
#define assertm(exp, msg) assert(((void)msg, exp))
typedef std::vector<int> int_vector;

typedef double my_float;
typedef std::complex<my_float> my_complex;

typedef std::vector<my_complex> complex_vector;
typedef std::vector<my_float> float_vector;
typedef std::vector<int> int_vector;

typedef std::vector<complex_vector> complex_matrix;
typedef std::vector<int_vector> int_matrix;

void scaleVectorInplace(std::vector<my_complex>& V, my_complex scale){
  std::transform(V.begin(), V.end(), V.begin(), [scale](auto s) { return s * scale; });
}

std::vector<my_complex> scaleVector(const std::vector<my_complex>& V, my_complex scale) {
  std::vector<my_complex> result = V;
  scaleVectorInplace(result, scale);
  return result;
}


my_complex qpskmod(int symbol) {
  const std::vector<my_complex> mapping {{1,1},  {-1,1}, {1,-1},  {-1, -1}};
  return mapping[symbol];
}

   int main() {

    int symbol;
  // Modulate one symbol to its QPSK representation
  const complex_vector mapping {{1,1},  {-1,1}, {1,-1},  {-1, -1}};

    int_vector symbols;
  // Modulate a vector of symbols with is QPSK representation
  std::vector<my_complex> result;
  //complex_vector result;

  result.resize(symbols.size());
  // applies QPSK modulation to each symbol in the vector
  std::transform(symbols.begin(), symbols.end(),
         result.begin(), [](int s) { return qpskmod(s); });
  return result;
}

Error:

In function 'int main()':
55:10: error: cannot convert 'std::vector<std::complex<double> >' to 'int' in return
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
YuliaM95
  • 35
  • 6
  • 4
    Think about what `int main()` is supposed to return and what you actually return in `return result;`. – Lukas-T Feb 27 '20 at 08:14
  • 3
    Please read the error message again. What does it say? What is the declared return type of the `main` function? What are you trying to return? Why are you trying to return that? What are you trying to do? – Some programmer dude Feb 27 '20 at 08:14
  • Are you possibly trying to print the contents of `result`? Then you need to explicitly do that in your program. – Some programmer dude Feb 27 '20 at 08:16
  • It doesn't make sense to *return* a vector from `main`. Perhaps you should get yourself [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start at the beginning. – molbdnilo Feb 27 '20 at 08:17
  • You are returning **result** wich is not an int. And **main** returns an int. – Abdelbaki Boukerche Feb 27 '20 at 08:17
  • I am trying to print the results, tried adding cout but showing an error. or how can I assign the values of qpskmod(s) and can I print it to the console? – YuliaM95 Feb 27 '20 at 08:17
  • @Someprogrammerdude yes, I am trying to do that. How could I prin them explicitly? can you please give an example. – YuliaM95 Feb 27 '20 at 08:20
  • 2
    You will have iterate manually over the vector and print each element, because there is no overload for `operator<<` with `std::vector`, i.e. you can't just push a vector an ostream. – Lukas-T Feb 27 '20 at 08:20
  • 1
    `for (auto value : result) { std::cout << value << '\n'; }` – Some programmer dude Feb 27 '20 at 08:26

1 Answers1

2

int main() returns a value of type int but you return the variable result of type std::vector<my_complex>

The return value of main indicates if the program terminated successfully (return 0) or with some kind of error code but no complex data structure

Catching up your other question from the comments how you can print the values, you might want to use a simple function for that like:

void printResult(std::vector<my_complex> &vec)
{
   std::vector<my_complex>::iterator itr = vec.begin();

   while (itr != vec.end())
   {
      std::cout << *itr << std::endl;
      itr++;
   }
}

The end of your main function could then look like:

printResult(result);
return EXIT_SUCCESS; // most likely same as return 0

You might also add a condition at the end for return EXIT_FAILURE instead

Odysseus
  • 1,213
  • 4
  • 12